001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.ideTools;
022:
023: /////////////////////////
024: //$Archive: /SOFIA/SourceCode/com/salmonllc/ideTools/ClassPathDialog.java $
025: //$Author: Srufle $
026: //$Revision: 5 $
027: //$Modtime: 4/15/03 3:11p $
028: /////////////////////////
029:
030: import java.awt.*;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.io.File;
034: import java.util.StringTokenizer;
035: import java.util.Vector;
036:
037: import javax.swing.*;
038: import javax.swing.event.ListSelectionEvent;
039: import javax.swing.event.ListSelectionListener;
040:
041: public class ClassPathDialog extends JDialog implements ActionListener,
042: ListSelectionListener {
043:
044: JList _list;
045: JScrollPane _pane;
046: String _paths;
047: JButton _delete, _moveUp, _moveDown;
048:
049: public ClassPathDialog(Frame owner, String text, boolean filesAlso) {
050: super (owner, filesAlso ? "Set Classpath" : "Set Path", true);
051:
052: int width = 620;
053: int height = 400;
054: Dimension frameBounds = Toolkit.getDefaultToolkit()
055: .getScreenSize();
056: int x = (frameBounds.width - width) / 2;
057: int y = (frameBounds.height - height) / 2;
058:
059: setBounds(x, y, width, height);
060:
061: JButton addFile = new JButton("Add File");
062: addFile.addActionListener(this );
063: JButton addDir = new JButton("Add Dir");
064: addDir.addActionListener(this );
065: JButton delete = new JButton("Delete");
066: delete.setEnabled(false);
067: delete.addActionListener(this );
068: JButton moveUp = new JButton("Move Up");
069: moveUp.setEnabled(false);
070: moveUp.addActionListener(this );
071: JButton moveDown = new JButton("Move Down");
072: moveDown.setEnabled(false);
073: moveDown.addActionListener(this );
074: JButton OK = new JButton("Save");
075: OK.addActionListener(this );
076: JButton close = new JButton("Cancel");
077: close.addActionListener(this );
078: JPanel buttonBar = new JPanel(new FlowLayout());
079:
080: _delete = delete;
081: _moveUp = moveUp;
082: _moveDown = moveDown;
083:
084: if (filesAlso)
085: buttonBar.add(addFile);
086:
087: buttonBar.add(addDir);
088: buttonBar.add(delete);
089: buttonBar.add(moveUp);
090: buttonBar.add(moveDown);
091: buttonBar.add(OK);
092: buttonBar.add(close);
093: buttonBar.setAlignmentX(Component.CENTER_ALIGNMENT);
094:
095: Vector v = new Vector();
096: StringTokenizer t = new StringTokenizer(text,
097: File.pathSeparator);
098: while (t.hasMoreTokens())
099: v.add(t.nextToken());
100:
101: _list = new JList(v);
102: _list.addListSelectionListener(this );
103: _pane = DialogComponentFactory.makeScrollPane(_list);
104: Dimension size = new Dimension(width, height - 100);
105: _pane.setPreferredSize(size);
106:
107: Box box = Box.createVerticalBox();
108: box.add(_pane);
109: box.add(Box.createRigidArea(new Dimension(10, 5)));
110: box.add(buttonBar);
111:
112: getContentPane().add(box);
113: setVisible(true);
114:
115: }
116:
117: public void actionPerformed(ActionEvent e) {
118: if (((JButton) e.getSource()).getText().equals("Save")) {
119: ListModel m = _list.getModel();
120: StringBuffer b = new StringBuffer();
121: for (int i = 0; i < m.getSize(); i++) {
122: b.append(m.getElementAt(i));
123: b.append(File.pathSeparatorChar);
124: }
125: _paths = b.toString();
126: setVisible(false);
127: } else if (((JButton) e.getSource()).getText().equals("Cancel")) {
128: _paths = null;
129: setVisible(false);
130: } else if (((JButton) e.getSource()).getText()
131: .equals("Move Up")) {
132: Vector v = new Vector();
133: ListModel m = _list.getModel();
134: int size = m.getSize();
135: int sel = _list.getSelectedIndex();
136: String val = (String) m.getElementAt(sel);
137: for (int i = 0; i < size; i++) {
138: if (i == (sel - 1)) {
139: v.add(val);
140: v.add(m.getElementAt(i));
141: } else if (i != sel)
142: v.add(m.getElementAt(i));
143: }
144: _list.setModel(new JList(v).getModel());
145: _list.setSelectedIndex(sel - 1);
146: } else if (((JButton) e.getSource()).getText().equals(
147: "Move Down")) {
148: Vector v = new Vector();
149: ListModel m = _list.getModel();
150: int size = m.getSize();
151: int sel = _list.getSelectedIndex();
152: String val = (String) m.getElementAt(sel);
153: for (int i = 0; i < size; i++) {
154: if (i == (sel + 1)) {
155: v.add(m.getElementAt(i));
156: v.add(val);
157: } else if (i != sel)
158: v.add(m.getElementAt(i));
159: }
160: _list.setModel(new JList(v).getModel());
161: _list.setSelectedIndex(sel + 1);
162: } else if (((JButton) e.getSource()).getText().equals("Delete")) {
163: Vector v = new Vector();
164: int size = _list.getModel().getSize();
165: for (int i = 0; i < size; i++) {
166: if (!_list.isSelectedIndex(i))
167: v.add(_list.getModel().getElementAt(i));
168: }
169: _list.setModel(new JList(v).getModel());
170: } else if (((JButton) e.getSource()).getText()
171: .startsWith("Add")) {
172: JFileChooser c = new JFileChooser();
173: if ((((JButton) e.getSource()).getText().endsWith("File"))) {
174: c.setDialogTitle("Add Files");
175: c.setFileSelectionMode(JFileChooser.FILES_ONLY);
176: } else {
177: c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
178: c.setDialogTitle("Add Directories");
179: }
180: c.setMultiSelectionEnabled(true);
181: ListModel m = _list.getModel();
182: if (m.getSize() > 0) {
183: String last = (String) m.getElementAt(m.getSize() - 1);
184: File f = new File(last);
185: File parent = f.getParentFile();
186: if (parent.exists())
187: c.setCurrentDirectory(parent);
188: }
189: if (c.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
190: File f[] = c.getSelectedFiles();
191: Vector v = new Vector(m.getSize());
192: for (int i = 0; i < m.getSize(); i++)
193: v.addElement(m.getElementAt(i));
194: for (int i = 0; i < f.length; i++) {
195: boolean found = false;
196: for (int j = 0; j < m.getSize(); j++) {
197: if (f[i].toString().equals(m.getElementAt(j))) {
198: found = true;
199: break;
200: }
201: }
202: if (!found)
203: v.addElement(f[i].toString());
204: }
205: _list.setModel(new JList(v).getModel());
206: }
207: }
208: }
209:
210: public String getPaths() {
211: return _paths;
212: }
213:
214: public void valueChanged(ListSelectionEvent e) {
215: _delete.setEnabled(_list.getSelectedIndex() > -1);
216: _moveUp.setEnabled(_list.getSelectedIndices().length == 1
217: && _list.getSelectedIndex() > 0);
218: _moveDown.setEnabled(_list.getSelectedIndices().length == 1
219: && _list.getSelectedIndex() < (_list.getModel()
220: .getSize() - 1));
221: }
222: }
|