2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.ui_data.filebrowser;
18 import java.awt.event.MouseAdapter;
19 import java.awt.event.MouseEvent;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Timer;
24 import java.util.TimerTask;
26 import javax.swing.ImageIcon;
27 import javax.swing.JButton;
28 import javax.swing.JMenuItem;
29 import javax.swing.JPopupMenu;
30 import javax.swing.SwingUtilities;
32 import org.scilab.modules.action_binding.InterpreterManagement;
33 import org.scilab.modules.commons.gui.FindIconHelper;
34 import org.scilab.modules.commons.gui.ScilabLAF;
35 import org.scilab.modules.gui.events.callback.CommonCallBack;
36 import org.scilab.modules.ui_data.utils.UiDataMessages;
39 * The File Browser history
40 * @author Calixte DENIZET
42 @SuppressWarnings(value = { "serial" })
43 public class ScilabFileBrowserHistory {
45 private static final String PREVIOUSICON = FindIconHelper.findIcon("go-previous");
46 private static final String NEXTICON = FindIconHelper.findIcon("go-next");
48 private final SwingScilabTreeTable stt;
49 private final List<String> history = new ArrayList<String>();
50 private int position = -1;
51 private final JButton previous;
52 private final JButton next;
53 private final JPopupMenu popup;
58 * @param stt the treetable associated with this history
60 public ScilabFileBrowserHistory(SwingScilabTreeTable stt) {
62 this.popup = new JPopupMenu();
63 this.popup.setBorderPainted(true);
65 previous = new JButton();
66 previous.setIcon(new ImageIcon(PREVIOUSICON));
67 ScilabLAF.setDefaultProperties(previous);
69 previous.setToolTipText(UiDataMessages.PREVIOUSDIR);
70 previous.addMouseListener(new MouseAdapter() {
72 public void mousePressed(MouseEvent e) {
75 timer.schedule(new TimerTask() {
78 if (!popup.isVisible() || popup.getInvoker() != next) {
87 public void mouseReleased(MouseEvent e) {
95 public void mouseClicked(MouseEvent e) {
96 if (SwingUtilities.isRightMouseButton(e) && previous.isEnabled() && (!popup.isVisible() || popup.getInvoker() != previous)) {
98 } else if (SwingUtilities.isLeftMouseButton(e) && !popup.isVisible() && previous.isEnabled()) {
99 ScilabFileBrowserHistory.this.stt.setBaseDir(history.get(position - 1), false);
100 chDir(history.get(position - 1));
101 setPositionInHistory(position - 1);
106 next = new JButton();
107 next.setIcon(new ImageIcon(NEXTICON));
108 ScilabLAF.setDefaultProperties(next);
110 next.setToolTipText(UiDataMessages.NEXTDIR);
111 next.addMouseListener(new MouseAdapter() {
113 public void mousePressed(MouseEvent e) {
116 timer.schedule(new TimerTask() {
119 if (!popup.isVisible() || popup.getInvoker() != next) {
128 public void mouseReleased(MouseEvent e) {
136 public void mouseClicked(MouseEvent e) {
137 if (SwingUtilities.isRightMouseButton(e) && next.isEnabled() && (!popup.isVisible() || popup.getInvoker() != next)) {
139 } else if (SwingUtilities.isLeftMouseButton(e) && !popup.isVisible() && next.isEnabled()) {
140 ScilabFileBrowserHistory.this.stt.setBaseDir(history.get(position + 1), false);
141 chDir(history.get(position + 1));
142 setPositionInHistory(position + 1);
151 * Show the popup under the button
152 * @param prev if true, the popup is show under the previous button
154 private void showPopup(boolean prev) {
157 for (int i = position - 1; i >= 0; i--) {
158 JMenuItem item = new JMenuItem(history.get(i));
160 item.addActionListener(new CommonCallBack(null) {
162 public void callBack() {
163 ScilabFileBrowserHistory.this.stt.setBaseDir(history.get(j), false);
164 chDir(history.get(j));
165 setPositionInHistory(j);
171 for (int i = position + 1; i < history.size(); i++) {
172 JMenuItem item = new JMenuItem(history.get(i));
174 item.addActionListener(new CommonCallBack(null) {
176 public void callBack() {
177 ScilabFileBrowserHistory.this.stt.setBaseDir(history.get(j), false);
178 chDir(history.get(j));
179 setPositionInHistory(j);
194 popup.show(button, 0, button.getBounds(null).height);
197 private static final void chDir(String path) {
198 File f = new File(path);
199 if (f.exists() && f.isDirectory() && f.canRead()) {
200 InterpreterManagement.requestScilabExec("chdir('" + path + "');");
205 * Add a path in the history
206 * @param path the path to add
208 public void addPathInHistory(String path) {
210 setPositionInHistory(position + 1);
214 * @return the previous button
216 public JButton getPreviousButton() {
221 * @return the next button
223 public JButton getNextButton() {
228 * Set the current position in the history
229 * @param pos the new position
231 private void setPositionInHistory(int pos) {
237 * Update the buttons state according to the position
238 * @param pos the position
240 private void updateButton(int pos) {
241 previous.setEnabled(history.size() >= 2 && pos != 0);
242 next.setEnabled(history.size() >= 2 && pos != history.size() - 1);