KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > editors > ClasspathFieldEditor


1 package com.bull.eclipse.jonas.editors;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7
8 /**
9  * Use in TomcatPreferencePage
10  * This class is based on PathEditor
11  * There is a button to add files on the list,
12  */

13
14 import java.io.File JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.DirectoryDialog;
23 import org.eclipse.swt.widgets.FileDialog;
24 import org.eclipse.swt.widgets.Widget;
25
26 import com.bull.eclipse.jonas.JonasPluginResources;
27
28 /**
29  * A field editor to edit directory paths.
30  */

31 public class ClasspathFieldEditor extends ListFieldEditor implements JonasPluginResources {
32
33     protected Button addJarZipButton;
34     protected Button addDirButton;
35
36     /**
37      * The last path, or <code>null</code> if none.
38      */

39     private String JavaDoc lastPath;
40
41     /**
42      * Creates a new path field editor
43      */

44     protected ClasspathFieldEditor() {
45     }
46
47
48     public ClasspathFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
49         init(name, labelText);
50         createControl(parent);
51     }
52
53     public ClasspathFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent, List JavaDoc extraList) {
54         init(name, labelText);
55         createControl(parent);
56         convertutilListtoWidgetList(extraList);
57         //JonasLauncherPlugin.log("Extra Resources Entries = " + list.getItem(0));
58
}
59     
60     protected String JavaDoc[] getNewJarZip() {
61         FileDialog dialog = new FileDialog(addJarZipButton.getShell(), SWT.MULTI);
62         if (lastPath != null) {
63             if (new File JavaDoc(lastPath).exists())
64                 dialog.setFilterPath(lastPath);
65         }
66         String JavaDoc file = dialog.open();
67     
68         if (dialog.getFileNames().length != 0) {
69             lastPath = dialog.getFilterPath();
70             String JavaDoc[] result = dialog.getFileNames();
71             for (int i = 0; i < result.length; i++) {
72                 result[i] = lastPath + File.separator + result[i];
73             }
74             return result;
75         } else {
76             return new String JavaDoc[0];
77         }
78     }
79
80     protected String JavaDoc getNewDir() {
81         DirectoryDialog dialog = new DirectoryDialog(addDirButton.getShell());
82         if (lastPath != null) {
83             if (new File JavaDoc(lastPath).exists())
84                 dialog.setFilterPath(lastPath);
85         }
86         String JavaDoc dir = dialog.open();
87         if (dir != null) {
88             dir = dir.trim();
89             if (dir.length() == 0)
90                 return null;
91             lastPath = dir;
92         }
93         return dir;
94     }
95
96     protected void createButtons(Composite buttonBox) {
97         addJarZipButton = createPushButton(buttonBox, PREF_PAGE_ADDJARZIPBUTTON_LABEL);//$NON-NLS-1$
98
addDirButton = createPushButton(buttonBox, PREF_PAGE_ADDDIRBUTTON_LABEL);//$NON-NLS-1$
99
removeButton = createPushButton(buttonBox, PREF_PAGE_REMOVEBUTTON_LABEL);//$NON-NLS-1$
100
upButton = createPushButton(buttonBox, PREF_PAGE_UPBUTTON_LABEL);//$NON-NLS-1$
101
downButton = createPushButton(buttonBox, PREF_PAGE_DOWNBUTTON_LABEL);//$NON-NLS-1$
102
}
103
104     /**
105      * Creates a selection listener.
106      */

107     public void createSelectionListener() {
108         selectionListener = new SelectionAdapter() {
109             public void widgetSelected(SelectionEvent event) {
110                 Widget widget = event.widget;
111                 if (widget == addJarZipButton) {
112                     addJarZipPressed();
113                 } else
114                     if (widget == addDirButton) {
115                         addDirPressed();
116                     } else
117                         if (widget == removeButton) {
118                             removePressed();
119                         } else
120                             if (widget == upButton) {
121                                 upPressed();
122                             } else
123                                 if (widget == downButton) {
124                                     downPressed();
125                                 } else
126                                     if (widget == list) {
127                                         selectionChanged();
128                                     }
129             }
130         };
131     }
132
133     protected void addJarZipPressed() {
134         setPresentsDefaultValue(false);
135         String JavaDoc[] input = getNewJarZip();
136     
137         for(int i=0; i<input.length; i++) {
138             if (input != null) {
139                 int index = list.getSelectionIndex();
140                 if (index >= 0)
141                     list.add(input[i], index + 1);
142                 else
143                     list.add(input[i], 0);
144                 selectionChanged();
145             }
146         }
147     }
148     
149     
150     protected void addDirPressed() {
151         setPresentsDefaultValue(false);
152         String JavaDoc input = getNewDir();
153     
154         if (input != null) {
155             int index = list.getSelectionIndex();
156             if (index >= 0)
157                 list.add(input, index + 1);
158             else
159                 list.add(input, 0);
160             selectionChanged();
161         }
162     }
163
164     protected void convertutilListtoWidgetList(java.util.List JavaDoc extraList) {
165         int i = 0;
166         while (i < extraList.size()) {
167             list.add((String JavaDoc)extraList.get(i));
168             i++;
169         }
170             
171     }
172 }
Popular Tags