View Javadoc

1   package twcsckernel.clientKernel.utils;
2   
3   import java.awt.Container;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.awt.event.MouseEvent;
7   import java.awt.event.MouseListener;
8   import java.io.IOException;
9   import java.rmi.RemoteException;
10  import java.text.ParseException;
11  import java.util.Vector;
12  
13  import javax.swing.Box;
14  import javax.swing.JComboBox;
15  import javax.swing.JFormattedTextField;
16  import javax.swing.JMenuItem;
17  import javax.swing.JOptionPane;
18  import javax.swing.JPanel;
19  import javax.swing.JPopupMenu;
20  import javax.swing.JScrollPane;
21  import javax.swing.JTable;
22  import javax.swing.JTextField;
23  import javax.swing.text.MaskFormatter;
24  
25  import twcsckernel.clientKernel.utils.fileChooserClasses.FileSystemModel;
26  import twcsckernel.clientKernel.utils.fileChooserClasses.FileSystemUtils;
27  import twcsckernel.projectbase.io.FileDescriptor;
28  import twcsckernel.projectbase.io.RemoteFile;
29  
30  /***
31   * 
32   * Narzędzie do graficznego wyboru plików zdalnych. Po utworzeniu nowej
33   * instancji wywolujemy metode <code>showDialog()</code>
34   * 
35   * @author VMDGroup
36   */
37  public class RemoteFileChooser extends JPanel implements MouseListener {
38  	/***
39  	 * 
40  	 */
41  	public static final int OK_OPTION = JOptionPane.OK_OPTION;
42  
43  	public static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION;
44  
45  	private static final long serialVersionUID = 1L;
46  
47  	private String filter;
48  
49  	private String filename = "";
50  
51  	private FileSystemModel fsm;
52  
53  	private final JTextField jtf = new JTextField();
54  
55  	private JTable fileTable;
56  
57  	private Vector<String> filterNames = new Vector<String>();
58  
59  	private Vector<String> filterRegExp = new Vector<String>();
60  
61  	private JComboBox jcb;
62  
63  	/***
64  	 * Otwiera okno dialogowe wyboru pliku. Po wybraniu pliku jego sciezke
65  	 * pobiera się za pomocą metody <i>getFilename().</i> Możliwe jest
66  	 * ustawienie filtru za pomocą metody <i>setFilter(String)</i>. Możliwa
67  	 * jest niespójność między wyświetlaną listą plików a stanem systemu plików
68  	 * na serwerze, jeśli nastąpiła jego zmiana w czasie kiedy bieżący katalog
69  	 * był wyświetlony. Możliwe jest więc wystąpienie błędu otwarcia zdalnego
70  	 * pliku.
71  	 * 
72  	 * @return Wartość zwracana : <code> RemoteFileChooser.OK_OPTION</code>,<code>RemoteFileChooser.CANCEL_OPTION</code>
73  	 */
74  	private int showDialog() {
75  		filename = null;
76  		fsm = new FileSystemModel("/", filterRegExp
77  				.get(filterRegExp.size() - 1));
78  		fileTable = new JTable(fsm);
79  		Container zawartosc = Box.createVerticalBox();
80  		fileTable.addMouseListener(this);
81  		JScrollPane jsp = new JScrollPane(fileTable);
82  		jsp.setSize(200, 200);
83  		zawartosc.add(jsp);
84  		zawartosc.add(jtf);
85  		jcb = new JComboBox(filterNames);
86  		jcb.setSelectedIndex(filterNames.size() - 1);
87  		jcb.addActionListener(new ActionListener() {
88  
89  			public void actionPerformed(ActionEvent e) {
90  				fsm.setFilterRegExp(filterRegExp.get(jcb.getSelectedIndex()));
91  				fsm.filter();
92  			}
93  
94  		});
95  		zawartosc.add(jcb);
96  		this.add(zawartosc);
97  		int wynik = OK_OPTION;
98  		while (wynik == OK_OPTION && filename == null) {
99  			wynik = JOptionPane.showOptionDialog(null, this, "Wybierz plik",
100 					JOptionPane.OK_CANCEL_OPTION,
101 					JOptionPane.INFORMATION_MESSAGE, null, null, null);
102 			if (!jtf.getText().equals("")) {
103 				filename = FileSystemUtils.preparePath(fsm.getCurrentDir()
104 						.concat("/" + jtf.getText()));
105 			}
106 		}
107 		return wynik;
108 	}
109 
110 	public int showOpenDialog() {
111 		jtf.setEditable(false);
112 		return showDialog();
113 	}
114 
115 	public int showSaveDialog() {
116 		jtf.setEditable(true);
117 		return showDialog();
118 	}
119 
120 	public RemoteFileChooser() {
121 		filterNames.add("Wszystkie pliki");
122 		filterRegExp.add(".*");
123 
124 	}
125 
126 	/***
127 	 * 
128 	 * @return Ciąg znaków okreslający bieżący filtr.
129 	 */
130 	public String getFilter() {
131 		return filter;
132 	}
133 
134 	/***
135 	 * Ustawianie filtru dla plików wyświetlanych. Domyślnie zawinstalowany jest
136 	 * filtr wyświetlający wszystkie pliki.
137 	 * 
138 	 * @param filtername -
139 	 *            określa nazwę dla filtru, np "Dokumenty word"
140 	 * @param filterRegExpr -
141 	 *            określa wyrażenie regularne określające wygląd nazwy pliku, np
142 	 *            <code>".*//.(?i)doc"
143 	 */
144 
145 	public void addFilter(String filtername, String filterRegExpr) {
146 		this.filterNames.add(filtername);
147 		this.filterRegExp.add(filterRegExpr);
148 
149 	}
150 
151 	/***
152 	 * 
153 	 * @return Pełna ścieżka do pliku zdalnego.
154 	 * 
155 	 */
156 
157 	public String getFilename() {
158 		return filename;
159 	}
160 
161 	public void mouseClicked(MouseEvent e) {
162 		if (e.getButton() == MouseEvent.BUTTON1) {
163 			FileDescriptor fd = fsm.getSelectedFiles(fileTable.rowAtPoint(e
164 					.getPoint()));
165 			if (!fd.isFile()) {
166 				jtf.setText("");
167 				String tmpstr = fsm.getCurrentDir().concat("/" + fd.getName());
168 				fsm.setCurrentDir(tmpstr);
169 				fsm.list();
170 				fsm.filter();
171 			} else {
172 				jtf.setText(fd.getName());
173 			}
174 		} else if (e.getButton() == MouseEvent.BUTTON3) {
175 
176 			JPopupMenu jpm = new JPopupMenu();
177 			JMenuItem item = new JMenuItem("Stwórz katalog");
178 			item.addActionListener(new ActionListener() {
179 				public void actionPerformed(ActionEvent e) {
180 					RemoteFile newDir = null;
181 					String answer = JOptionPane.showInputDialog(fileTable,
182 							"Podaj nazwę katalogu", "Nowy katalog...",
183 							JOptionPane.INFORMATION_MESSAGE);
184 					if (answer != null && answer.length() != 0) {
185 						try {
186 							newDir = CommonVariablesContainer.rff
187 									.newRemoteFile(fsm.getCurrentDir() + answer);
188 						} catch (RemoteException e1) {
189 							return;
190 						} catch (SecurityException e1) {
191 							return;
192 						} catch (IOException e1) {
193 							return;
194 						}
195 						if (newDir.mkdir()) {
196 							fsm.list();
197 							fsm.filter();
198 						}
199 						newDir.disposeFileHandle();
200 					} else
201 						return;
202 
203 				}
204 			});
205 			jpm.add(item);
206 			jpm.show(fileTable, e.getX(), e.getY());
207 		}
208 
209 	}
210 
211 	public void mousePressed(MouseEvent e) {
212 		// TODO Auto-generated method stub
213 
214 	}
215 
216 	public void mouseReleased(MouseEvent e) {
217 		// TODO Auto-generated method stub
218 
219 	}
220 
221 	public void mouseEntered(MouseEvent e) {
222 		// TODO Auto-generated method stub
223 
224 	}
225 
226 	public void mouseExited(MouseEvent e) {
227 		// TODO Auto-generated method stub
228 
229 	}
230 
231 }