PageRenderTime 114ms CodeModel.GetById 76ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 1ms

/bundles/plugins-trunk/CommonControls/common/gui/pathbuilder/PathBuilder.java

#
Java | 590 lines | 306 code | 78 blank | 206 comment | 72 complexity | 0fb7c0eb17fffa43c7a1d1b9c2e82592 MD5 | raw file
  1package common.gui.pathbuilder;
  2
  3/*
  4 * PathBuilder.java
  5 * Part of the JSwat plugin for the jEdit text editor
  6 * Copyright (C) 2001 David Taylor
  7 * dtaylo11@bigpond.net.au
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24import java.awt.*;
 25import java.awt.event.*;
 26import java.io.File;
 27import java.util.*;
 28import javax.swing.*;
 29import javax.swing.event.ListSelectionListener;
 30import javax.swing.event.ListSelectionEvent;
 31import javax.swing.filechooser.*;
 32import javax.swing.table.*;
 33import javax.swing.text.*;
 34
 35import org.gjt.sp.jedit.jEdit;
 36
 37/** A component that allows a user to build a
 38 * classpath by selecting directories files using a filesystem
 39 * browser.<p>
 40 */
 41public class PathBuilder extends JPanel implements ActionListener, ListSelectionListener {
 42    /**
 43     * The table that displays the path elements.<p>
 44     */
 45    private JTable pathElementTable;
 46
 47    /**
 48     * The table model that holds the path elements.<p>
 49     */
 50    private PathElementTableModel pathElementModel;
 51
 52    /**
 53     * A panel to hold the buttons.<p>
 54     */
 55    private JPanel btnPanel;
 56
 57    /**
 58     * The button to add an element.<p>
 59     */
 60    private JButton addElement;
 61
 62    /**
 63     * The button to remove an element.<p>
 64     */
 65    private JButton removeElement;
 66
 67    /**
 68     * The button to move an element towards the front of the path.<p>
 69     */
 70    private JButton moveUp;
 71
 72    /**
 73     * The button to move an element towards the end of the path.<p>
 74     */
 75    private JButton moveDown;
 76
 77    /**
 78     * Whether the move buttons are enabled or not.<p>
 79     */
 80    private boolean moveButtonsEnabled = true;
 81
 82    /**
 83     * Whether to enabled multi-selection in the file chooser or not.<p>
 84     */
 85    private boolean multiSelectionEnabled = false;
 86
 87    /**
 88     * The elements of the path.<p>
 89     */
 90    private Vector elements;
 91
 92    /**
 93     * The initial directory to show in the file dialog.<p>
 94     */
 95    private String startDirectory;
 96
 97    /**
 98     * The file selection mode. By default it is FILES_AND_DIRECTORIES.<p>
 99     */
100    private int fileSelectionMode;
101
102    /**
103     * A list of external action listeners added by users of this class.
104     */
105    private java.util.List<ActionListener> actionListeners = null;
106
107    /**
108     * A file filter to set on the file chooser.<p>
109     */
110    private FileFilter filter;
111
112    private static final String PROPS_PREFIX = "common.gui.pathbuilder";
113
114    private String addButtonText;
115    private String removeButtonText;
116    private String moveUpButtonText;
117    private String moveDownButtonText;
118    private String fileDialogTitle;
119    private String fileDialogAction;
120
121    /**
122     * Creates a new PathBuilder.  Title at the top of the path table will be
123     * "Classpath Elements".
124     *
125     */
126    public PathBuilder() {
127        this("Classpath Elements");
128    }
129
130    /**
131     * Creates a new PathBuilder.<p>
132     *
133     * @param title The title to display at the top of the path table.
134     */
135    public PathBuilder(String title) {
136        super(new BorderLayout());
137
138        elements = new Vector();
139        pathElementModel = new PathElementTableModel(title);
140
141        addButtonText = jEdit.getProperty(PROPS_PREFIX + ".addButtonText");
142        removeButtonText = jEdit.getProperty(PROPS_PREFIX + ".removeButtonText");
143        moveUpButtonText = jEdit.getProperty(PROPS_PREFIX + ".moveUpButtonText");
144        moveDownButtonText = jEdit.getProperty(PROPS_PREFIX + ".moveDownButtonText");
145        fileDialogTitle = jEdit.getProperty(PROPS_PREFIX + ".fileDialogTitle");
146        fileDialogAction = jEdit.getProperty(PROPS_PREFIX + ".fileDialogAction");
147
148        addElement = new JButton(addButtonText);
149        addElement.addActionListener(this);
150
151        removeElement = new JButton(removeButtonText);
152        removeElement.addActionListener(this);
153
154        moveUp = new JButton(moveUpButtonText);
155        moveUp.addActionListener(this);
156
157        moveDown = new JButton(moveDownButtonText);
158        moveDown.addActionListener(this);
159
160        btnPanel = new JPanel();
161        btnPanel.setBorder(BorderFactory.createEmptyBorder(6, 0, 0, 0));
162        btnPanel.add(addElement);
163        btnPanel.add(removeElement);
164        btnPanel.add(moveUp);
165        btnPanel.add(moveDown);
166        add(btnPanel, BorderLayout.SOUTH);
167
168        removeElement.setEnabled(false);
169        moveUp.setEnabled(false);
170        moveDown.setEnabled(false);
171
172        pathElementTable = new JTable(pathElementModel);
173        JScrollPane tableScroller = new JScrollPane(pathElementTable);
174        add(tableScroller, BorderLayout.CENTER);
175
176        pathElementTable.getSelectionModel().addListSelectionListener(this);
177        if(elements.size() > 0)
178            pathElementTable.setRowSelectionInterval(0, 0);
179
180        fileSelectionMode = JFileChooser.FILES_AND_DIRECTORIES;
181    }
182
183    /**
184     * Set the text of the add element button.<p>
185     *
186     * @param text the String to display on the add element button.
187     */
188    public void setAddButtonText(String text) {
189        //addButtonText = text;
190        addElement.setText(text);
191    }
192
193    /**
194     * Set the text of the remove element button.<p>
195     *
196     * @param text the String to display on the remove element button.
197     */
198    public void setRemoveButtonText(String text) {
199        //removeButtonText = text;
200        removeElement.setText(text);
201    }
202
203    /**
204     * Set the text of the move up button.<p>
205     *
206     * @param text the String to display on the move up button.
207     */
208    public void setMoveUpButtonText(String text) {
209        //moveUpButtonText = text;
210        moveUp.setText(text);
211    }
212
213    /**
214     * Set the text of the move down button.<p>
215     *
216     * @param text the String to display on the move down button.
217     */
218    public void setMoveDownButtonText(String text) {
219        //moveDownButtonText = text;
220        moveDown.setText(text);
221    }
222
223    /**
224     * Set a file selection mode to customise type of files can be selected.<p>
225     *
226     * @param filter the filter to use.
227     */
228    public void setFileSelectionMode(int fsm) {
229        this.fileSelectionMode = fsm;
230    }
231
232    /**
233     * Set a filter to customise what files are displayed.<p>
234     *
235     * @param filter the filter to use.
236     */
237    public void setFileFilter(FileFilter filter) {
238        this.filter = filter;
239    }
240
241    /**
242     * Sets the initial directory to be displayed by the file dialog.<p>
243     *
244     * @param startDirectory the initial directory to be displayed by the
245     * file dialog.
246     */
247    public void setStartDirectory(String startDirectory) {
248        this.startDirectory = startDirectory;
249    }
250
251    /**
252     * Sets the title of the file dialog.<p>
253     *
254     * @param fileDialogTitle the title of the file dialog.
255     */
256    public void setFileDialogTitle(String fileDialogTitle) {
257        this.fileDialogTitle = fileDialogTitle;
258    }
259
260    /**
261     * Sets the label of the file dialog "approve" button.<p>
262     *
263     * @param fileDialogAction the label of the file dialog "approve" button.
264     */
265    public void setFileDialogAction(String fileDialogAction) {
266        this.fileDialogAction = fileDialogAction;
267    }
268
269    /**
270     * Set the path to be displayed in the list box.<p>
271     *
272     * @param path the current path elements, separated by
273     * File.pathSeparator.
274     */
275    public void setPath(String path) {
276        int size = elements.size();
277        if(size > 0)
278        {
279            elements.clear();
280            pathElementModel.fireTableRowsDeleted(0, size - 1);
281        }
282
283        StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
284        while(st.hasMoreTokens()) {
285            elements.addElement(st.nextToken());
286        }
287
288        if(elements.size() > 0) {
289            pathElementModel.fireTableRowsInserted(0, elements.size() - 1);
290            pathElementTable.setRowSelectionInterval(0, 0);
291        }
292    }
293
294    /**
295     * Set the path to be displayed in the list box.<p>
296     *
297     * @param path an array of the current path elements.
298     */
299    public void setPathArray(String[] path) {
300        int size = elements.size();
301        if ( size > 0 ) {
302            elements.clear();
303            pathElementModel.fireTableRowsDeleted(0, size - 1);
304        }
305
306        for(int i = 0; i < path.length; i++)
307            elements.addElement(path[i]);
308
309        if(elements.size() > 0) {
310            pathElementModel.fireTableRowsInserted(0, elements.size() - 1);
311            pathElementTable.setRowSelectionInterval(0, 0);
312        }
313    }
314
315    /**
316     * Returns the path built using this PathBuilder as a single String,
317     * with the elements of the path separated by File.pathSeparator.<p>
318     *
319     * @return the path built using this PathBuilder.
320     */
321    public String getPath() {
322        StringBuffer sb = new StringBuffer();
323        for(int i = 0; i < elements.size(); i++) {
324            sb.append((String)elements.elementAt(i));
325            if(i < (elements.size() - 1))
326                sb.append(File.pathSeparator);
327        }
328
329        return sb.toString();
330    }
331
332    /**
333     * Returns the path built using this PathBuilder as an array of
334     * Strings.<p>
335     *
336     * @return the path built using this PathBuilder.
337     */
338    public String[] getPathArray() {
339        String[] pathArray = new String[elements.size()];
340        for(int i = 0; i < elements.size(); i++)
341            pathArray[i] = (String)elements.elementAt(i);
342
343        return pathArray;
344    }
345
346    /**
347     * Returns the last directory selected in the file chooser dialog
348     *
349     * @return the last selected directory
350     */
351    public String getStartDirectory() {
352        return startDirectory;
353    }
354
355    /**
356     * Enable or disable the move buttons.<p>
357     *
358     * @param enabled true to enabled the move up and move down buttons,
359     * false to hide them.
360     */
361    public void setMoveButtonsEnabled(boolean enabled) {
362        if(enabled == true && moveButtonsEnabled == false) {
363            moveButtonsEnabled = true;
364            btnPanel.add(moveUp);
365            btnPanel.add(moveDown);
366        }
367        else if(enabled == false && moveButtonsEnabled == true) {
368            moveButtonsEnabled = false;
369            btnPanel.remove(moveDown);
370            btnPanel.remove(moveUp);
371        }
372    }
373
374    /**
375     * Enable or disable multiple file selection in the file chooser.<p>
376     *
377     * @param multiSelectionEnabled true to enable multiple file selection,
378     * false to disable it.
379     */
380    public void setMultiSelectionEnabled(boolean multiSelectionEnabled) {
381        this.multiSelectionEnabled = multiSelectionEnabled;
382    }
383
384    /**
385     * Listen to specific GUI events.
386     *
387     * @param evt the GUI event.
388     */
389    public void actionPerformed(ActionEvent evt) {
390        int row;
391        Object source = evt.getSource();
392        if(source.equals(addElement)) {
393            JFileChooser chooser;
394            if(startDirectory != null)
395                chooser = new JFileChooser(startDirectory);
396            else
397                chooser = new JFileChooser();
398
399            chooser.setFileSelectionMode(fileSelectionMode);
400            if(multiSelectionEnabled == true)
401                chooser.setMultiSelectionEnabled(true);
402
403            if(filter != null)
404                chooser.addChoosableFileFilter(filter);
405
406            chooser.setDialogTitle(fileDialogTitle);
407            int returnVal = chooser.showDialog(null, fileDialogAction);
408            if(returnVal == JFileChooser.APPROVE_OPTION) {
409		    if(multiSelectionEnabled == true) {
410		        File[] files = chooser.getSelectedFiles();
411		        for(int i = 0; i < files.length; i++)
412		            pathElementModel.add(files[i].getPath());
413		    } else
414		        pathElementModel.add(chooser.getSelectedFile().getPath());
415
416		    if(elements.size() == 1)
417		        pathElementTable.setRowSelectionInterval(0, 0);
418
419		    startDirectory = chooser.getCurrentDirectory().getPath();
420            }
421        }
422        else if(source.equals(removeElement)) {
423            row = pathElementTable.getSelectedRow();
424            if(row >= 0) {
425                pathElementModel.remove(row);
426            }
427        }
428        else if(source.equals(moveUp)) {
429            row = pathElementTable.getSelectedRow();
430            if(row >= 1) {
431                pathElementModel.moveUp(row);
432            }
433        }
434        else if(source.equals(moveDown)) {
435            row = pathElementTable.getSelectedRow();
436            if(row < (elements.size() - 1)) {
437                pathElementModel.moveDown(row);
438            }
439        }
440
441        int tableSize = elements.size();
442        if(tableSize < 1 && removeElement.isEnabled())
443            removeElement.setEnabled(false);
444        else if(tableSize > 0 && removeElement.isEnabled() != true)
445            removeElement.setEnabled(true);
446
447        // update the move up/down buttons
448        valueChanged(null);
449
450        // notify external action listeners
451        if (actionListeners != null) {
452            for (ActionListener actionListener : actionListeners) {
453                actionListener.actionPerformed(new ActionEvent(this, 0, ""));
454            }
455        }
456    }
457
458    /**
459     * Handle list selection events.<p>
460     *
461     * @param evt the list selection event.
462     */
463    public void valueChanged(ListSelectionEvent evt) {
464        int row = pathElementTable.getSelectedRow();
465        int tableSize = elements.size();
466
467        if(tableSize < 1 && removeElement.isEnabled())
468            removeElement.setEnabled(false);
469        else if(tableSize > 0 && removeElement.isEnabled() != true)
470            removeElement.setEnabled(true);
471
472        if(tableSize < 1) {
473            moveUp.setEnabled(false);
474            moveDown.setEnabled(false);
475            return;
476        }
477
478        if(row < 1) {
479            moveUp.setEnabled(false);
480            if(tableSize > 1 && moveDown.isEnabled() != true)
481                moveDown.setEnabled(true);
482        }
483        else if(row == (tableSize - 1)) {
484            moveDown.setEnabled(false);
485            if(moveUp.isEnabled() != true)
486                moveUp.setEnabled(true);
487        }
488        else {
489            moveUp.setEnabled(true);
490            moveDown.setEnabled(true);
491        }
492    }
493
494    /**
495     * Users of this class may add action listeners to be notified when
496     * paths are changed.  Each action listener is called when ever the
497     * 'add', 'remove', or 'move' buttons are pressed.
498     */
499    public void addActionListener(ActionListener listener) {
500        if (actionListeners == null) {
501            actionListeners = new ArrayList<ActionListener>();
502        }
503        actionListeners.add(listener);
504    }
505
506    /**
507     * A simple table model of the classpathElementTable.<p>
508     */
509    class PathElementTableModel extends AbstractTableModel {
510        private String title = "";
511        public PathElementTableModel(String title) {
512            this.title = title;
513        }
514
515        public int getRowCount() {
516            return elements.size();
517        }
518
519        public int getColumnCount() {
520            return 1;
521        }
522
523        public String getColumnName(int column) {
524            return title;
525        }
526
527        public Object getValueAt(int row, int column) {
528            return elements.elementAt(row);
529        }
530
531        /**
532         * Add an element to the path model.<p>
533         *
534         * @param value the path element to be added.
535         */
536        protected void add(String value) {
537            int rows = elements.size();
538            elements.addElement(value);
539            fireTableRowsInserted(rows, rows);
540        }
541
542        /**
543         * Remove an element from the path model.<p>
544         *
545         * @param row the index of the element to remove.
546         */
547        protected void remove(int row) {
548            elements.removeElementAt(row);
549            fireTableRowsDeleted(row, row);
550            if(elements.size() > 0) {
551                if(elements.size() > row) {
552                    pathElementTable.setRowSelectionInterval(row, row);
553                }
554                else {
555                    row = elements.size() - 1;
556                    pathElementTable.setRowSelectionInterval(row, row);
557                }
558            }
559        }
560
561        /**
562         * Move an element up (towards the front of) the path.<p>
563         *
564         * @param row the element to be moved.
565         */
566        protected void moveUp(int row) {
567            Object a = elements.elementAt(row);
568            Object b = elements.elementAt(row - 1);
569            elements.setElementAt(a, row - 1);
570            elements.setElementAt(b, row);
571            fireTableRowsUpdated(row - 1, row);
572            pathElementTable.setRowSelectionInterval(row - 1, row - 1);
573        }
574
575        /**
576         * Move an element down (towards the end of) the path.<p>
577         *
578         * @param row the element to be moved.
579         */
580        protected void moveDown(int row) {
581            Object a = elements.elementAt(row);
582            Object b = elements.elementAt(row + 1);
583            elements.setElementAt(a, row + 1);
584            elements.setElementAt(b, row);
585            fireTableRowsUpdated(row, row + 1);
586            pathElementTable.setRowSelectionInterval(row + 1, row + 1);
587        }
588    }
589}
590