001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.RFileBrowser;
020
021import java.awt.Image;
022import java.awt.Toolkit;
023import java.rmi.RemoteException;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import javax.swing.DefaultListModel;
029import javax.swing.plaf.metal.MetalIconFactory;
030
031/**
032 * Remote File Chooser window
033 *
034 * @author Samuel Campos <samuelcampos@ua.pt>
035 */
036@Deprecated
037public class RemoteFileChooser extends javax.swing.JFrame {
038
039    public static final int APPROVED = 1;
040    public static final int CANCELED = 2;
041    public static final int DIRECTORIES_ONLY = 3;
042    
043    private IRemoteFileSystem rFS;
044    private String actualPath;
045    private int mode;
046    private FileAction act;
047    private boolean created = false;
048
049    /** Creates new form FileChooser */
050    public RemoteFileChooser(IRemoteFileSystem rFS, String homePath, FileAction action) {
051        initComponents();
052
053        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
054        this.setIconImage(image);
055
056        jButtonUP.setIcon(MetalIconFactory.getFileChooserUpFolderIcon());
057        jButtonHome.setIcon(MetalIconFactory.getFileChooserHomeFolderIcon());
058
059        jListFiles.setCellRenderer(new IconListRenderer());
060        jComboBox.setRenderer(new IconListRenderer());
061
062        if (homePath == null) {
063            homePath = ".";
064        }
065
066        this.rFS = rFS;
067        actualPath = homePath;
068
069        mode = 0;
070
071        loadFiles();
072
073        created = true;
074
075        act = action;
076    }
077
078    /**
079     * Load Files to JListFiles
080     */
081    private void loadFiles() {
082        try {
083            refreshComboBox();
084
085            RemoteFile[] files = rFS.getFiles(actualPath, false);
086            DefaultListModel list = new DefaultListModel();
087
088            for (int i = 0; i < files.length; i++) {
089                if (mode == 0 || (mode == DIRECTORIES_ONLY && files[i].isDirectory())) {
090                    list.addElement(files[i]);
091                }
092            }
093
094            // (files.length + 1) / 2 - to round up
095            jListFiles.setVisibleRowCount((files.length + 1) / 2);
096
097            jListFiles.setModel(list);
098
099            jTextFieldFilePath.setText(actualPath);
100        } catch (RemoteException ex) {
101            LoggerFactory.getLogger(RemoteFileChooser.class).error(ex.getMessage(), ex);
102        }
103    }
104
105    private void refreshComboBox() {
106        try {
107            jComboBox.removeAllItems();
108            RemoteFile[] rFiles = rFS.getFilePath(actualPath);
109            RemoteFile tmp;
110            int i;
111
112            // invert the list order
113            for (i = 0; i < rFiles.length / 2; i++) {
114                tmp = rFiles[i];
115                rFiles[i] = rFiles[rFiles.length - 1 - i];
116                rFiles[rFiles.length - 1 - i] = tmp;
117            }
118
119            for (i = 0; i < rFiles.length; i++) {
120                jComboBox.insertItemAt(rFiles[i], i);
121            }
122
123            jComboBox.setSelectedIndex(rFiles.length - 1);
124        } catch (RemoteException ex) {
125            LoggerFactory.getLogger(RemoteFileChooser.class).error(ex.getMessage(), ex);
126        }
127    }
128
129    public void setFileSelectionMode(int mode) {
130        if (mode == DIRECTORIES_ONLY) {
131            this.mode = mode;
132        }
133    }
134
135    /** This method is called from within the constructor to
136     * initialize the form.
137     * WARNING: Do NOT modify this code. The content of this method is
138     * always regenerated by the Form Editor.
139     */
140    @SuppressWarnings("unchecked")
141    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
142    private void initComponents() {
143
144        jLabel2 = new javax.swing.JLabel();
145        jLabel1 = new javax.swing.JLabel();
146        jComboBox = new javax.swing.JComboBox();
147        jSeparator1 = new javax.swing.JSeparator();
148        jSeparator2 = new javax.swing.JSeparator();
149        jLabel3 = new javax.swing.JLabel();
150        jTextFieldFilePath = new javax.swing.JTextField();
151        jButtonOpen = new javax.swing.JButton();
152        jScrollPane1 = new javax.swing.JScrollPane();
153        jListFiles = new javax.swing.JList();
154        jButtonCancel = new javax.swing.JButton();
155        jButtonUP = new javax.swing.JButton();
156        jButtonHome = new javax.swing.JButton();
157
158        jLabel2.setText("Look in:");
159
160        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
161        setMinimumSize(new java.awt.Dimension(571, 320));
162
163        jLabel1.setText("Look in:");
164
165        jComboBox.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
166            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
167            }
168            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
169                jComboBoxPopupMenuWillBecomeInvisible(evt);
170            }
171            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
172            }
173        });
174
175        jLabel3.setText("File Name:");
176
177        jButtonOpen.setText("Open");
178        jButtonOpen.addActionListener(new java.awt.event.ActionListener() {
179            public void actionPerformed(java.awt.event.ActionEvent evt) {
180                jButtonOpenActionPerformed(evt);
181            }
182        });
183
184        jListFiles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
185        jListFiles.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
186        jListFiles.setDoubleBuffered(true);
187        jListFiles.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
188        jListFiles.addMouseListener(new java.awt.event.MouseAdapter() {
189            public void mouseClicked(java.awt.event.MouseEvent evt) {
190                jListFilesMouseClicked(evt);
191            }
192        });
193        jListFiles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
194            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
195                jListFilesValueChanged(evt);
196            }
197        });
198        jScrollPane1.setViewportView(jListFiles);
199
200        jButtonCancel.setText("Cancel");
201        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
202            public void actionPerformed(java.awt.event.ActionEvent evt) {
203                jButtonCancelActionPerformed(evt);
204            }
205        });
206
207        jButtonUP.setText("UP");
208        jButtonUP.addActionListener(new java.awt.event.ActionListener() {
209            public void actionPerformed(java.awt.event.ActionEvent evt) {
210                jButtonUPActionPerformed(evt);
211            }
212        });
213
214        jButtonHome.setText("Home");
215        jButtonHome.addActionListener(new java.awt.event.ActionListener() {
216            public void actionPerformed(java.awt.event.ActionEvent evt) {
217                jButtonHomeActionPerformed(evt);
218            }
219        });
220
221        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
222        getContentPane().setLayout(layout);
223        layout.setHorizontalGroup(
224            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
225            .add(layout.createSequentialGroup()
226                .addContainerGap()
227                .add(jLabel1)
228                .add(34, 34, 34)
229                .add(jComboBox, 0, 274, Short.MAX_VALUE)
230                .add(192, 192, 192))
231            .add(layout.createSequentialGroup()
232                .addContainerGap()
233                .add(jLabel3)
234                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
235                .add(jTextFieldFilePath, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
236                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
237                .add(jButtonOpen)
238                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
239                .add(jButtonCancel)
240                .addContainerGap())
241            .add(layout.createSequentialGroup()
242                .add(12, 12, 12)
243                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
244                    .add(layout.createSequentialGroup()
245                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
246                        .addContainerGap())
247                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
248                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
249                            .add(layout.createSequentialGroup()
250                                .add(jButtonUP)
251                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
252                                .add(jButtonHome))
253                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE))
254                        .add(20, 20, 20))))
255            .add(layout.createSequentialGroup()
256                .add(36, 36, 36)
257                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
258                .add(60, 60, 60))
259        );
260        layout.setVerticalGroup(
261            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
262            .add(layout.createSequentialGroup()
263                .add(17, 17, 17)
264                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
265                    .add(jLabel1)
266                    .add(jComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
267                    .add(jButtonHome)
268                    .add(jButtonUP))
269                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
270                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
271                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
272                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
273                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
274                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
275                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
276                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
277                    .add(jLabel3)
278                    .add(jTextFieldFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
279                    .add(jButtonOpen)
280                    .add(jButtonCancel))
281                .add(40, 40, 40))
282        );
283
284        pack();
285    }// </editor-fold>//GEN-END:initComponents
286
287    private void jListFilesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListFilesValueChanged
288        if (!jListFiles.isSelectionEmpty()) {
289            RemoteFile file = (RemoteFile) jListFiles.getSelectedValue();
290            jTextFieldFilePath.setText(file.getPath());
291        }
292    }//GEN-LAST:event_jListFilesValueChanged
293
294    private void jButtonUPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUPActionPerformed
295        try {
296            if (!rFS.isFileSystemRoot(actualPath)) {
297                RemoteFile file = rFS.getParentDirectory(actualPath);
298
299                if(file != null){
300                    actualPath = file.getPath();
301
302                    loadFiles();
303                }
304            }
305        } catch (RemoteException ex) {
306            LoggerFactory.getLogger(RemoteFileChooser.class).error(ex.getMessage(), ex);
307        }
308    }//GEN-LAST:event_jButtonUPActionPerformed
309
310    private void jListFilesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListFilesMouseClicked
311        if (evt.getClickCount() == 2) {
312            RemoteFile file = (RemoteFile) jListFiles.getSelectedValue();
313            if (file.isDirectory()) {
314                actualPath = file.getPath();
315                loadFiles();
316            }
317        }
318    }//GEN-LAST:event_jListFilesMouseClicked
319
320    private void jButtonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenActionPerformed
321        if (act != null && jTextFieldFilePath.getText() != null) {
322            act.setFileChoosed(jTextFieldFilePath.getText());
323        }
324
325        this.dispose();
326    }//GEN-LAST:event_jButtonOpenActionPerformed
327
328    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
329        this.dispose();
330    }//GEN-LAST:event_jButtonCancelActionPerformed
331
332    private void jComboBoxPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxPopupMenuWillBecomeInvisible
333        try {
334            int index = jComboBox.getSelectedIndex();
335            RemoteFile[] rFiles = rFS.getFilePath(actualPath);
336            actualPath = rFiles[rFiles.length - 1 - index].getPath();
337
338            loadFiles();
339        } catch (RemoteException ex) {
340            LoggerFactory.getLogger(RemoteFileChooser.class).error(ex.getMessage(), ex);
341        }
342    }//GEN-LAST:event_jComboBoxPopupMenuWillBecomeInvisible
343
344    private void jButtonHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHomeActionPerformed
345        try {
346            if (!rFS.isFileSystemRoot(actualPath)) {
347                RemoteFile file = rFS.getHomeDirectory();
348                actualPath = file.getPath();
349
350                loadFiles();
351            }
352        } catch (RemoteException ex) {
353            LoggerFactory.getLogger(RemoteFileChooser.class).error(ex.getMessage(), ex);
354        }
355    }//GEN-LAST:event_jButtonHomeActionPerformed
356
357    // Variables declaration - do not modify//GEN-BEGIN:variables
358    private javax.swing.JButton jButtonCancel;
359    private javax.swing.JButton jButtonHome;
360    private javax.swing.JButton jButtonOpen;
361    private javax.swing.JButton jButtonUP;
362    private javax.swing.JComboBox jComboBox;
363    private javax.swing.JLabel jLabel1;
364    private javax.swing.JLabel jLabel2;
365    private javax.swing.JLabel jLabel3;
366    private javax.swing.JList jListFiles;
367    private javax.swing.JScrollPane jScrollPane1;
368    private javax.swing.JSeparator jSeparator1;
369    private javax.swing.JSeparator jSeparator2;
370    private javax.swing.JTextField jTextFieldFilePath;
371    // End of variables declaration//GEN-END:variables
372}