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.client.windows;
020
021import java.awt.Image;
022import java.awt.Toolkit;
023import java.rmi.RemoteException;
024import java.util.ArrayList;
025import java.util.Enumeration;
026import java.util.concurrent.Semaphore;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import javax.swing.DefaultListModel;
032import javax.swing.JOptionPane;
033import pt.ua.dicoogle.rGUI.client.AdminRefs;
034import pt.ua.dicoogle.rGUI.interfaces.controllers.IPendingMessages;
035
036/**
037 *
038 * @author Samuel Campos <samuelcampos@ua.pt>
039 */
040@Deprecated
041public class FileAlreadyIndexed extends javax.swing.JFrame {
042
043    private static IPendingMessages pendingMessages;
044
045    private static Semaphore sem = new Semaphore(1, true);
046    private static FileAlreadyIndexed instance;
047
048    public static FileAlreadyIndexed getInstance() {
049        try {
050            sem.acquire();
051            if (instance == null) {
052                instance = new FileAlreadyIndexed();
053            }
054            sem.release();
055        } catch (InterruptedException ex) {
056            LoggerFactory.getLogger(FileAlreadyIndexed.class).error(ex.getMessage(), ex);
057        }
058        return instance;
059    }
060
061    /** Creates new form FileAlreadyIndexed */
062    private FileAlreadyIndexed() {
063        initComponents();
064
065        this.setTitle("Re-Index Files");
066
067        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
068        this.setIconImage(image);
069
070        jLabelNumber.setText("0");
071        
072        jListIndexedFiles.setModel(new DefaultListModel());
073
074        pendingMessages = AdminRefs.getInstance().getPendingMessages();
075    }
076
077    /** This method is called from within the constructor to
078     * initialize the form.
079     * WARNING: Do NOT modify this code. The content of this method is
080     * always regenerated by the Form Editor.
081     */
082    @SuppressWarnings("unchecked")
083    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
084    private void initComponents() {
085
086        jLabel1 = new javax.swing.JLabel();
087        jLabel2 = new javax.swing.JLabel();
088        jScrollPane1 = new javax.swing.JScrollPane();
089        jListIndexedFiles = new javax.swing.JList();
090        jButtonYes = new javax.swing.JButton();
091        jButtonNo = new javax.swing.JButton();
092        jCheckBoxApplyAll = new javax.swing.JCheckBox();
093        jLabelNumber = new javax.swing.JLabel();
094        jLabel3 = new javax.swing.JLabel();
095
096        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
097        addWindowListener(new java.awt.event.WindowAdapter() {
098            public void windowClosing(java.awt.event.WindowEvent evt) {
099                formWindowClosing(evt);
100            }
101        });
102
103        jLabel1.setText("The files listed bellow are already indexed:");
104
105        jLabel2.setText("Do you want to index them again?");
106
107        jScrollPane1.setViewportView(jListIndexedFiles);
108
109        jButtonYes.setText("Yes");
110        jButtonYes.addActionListener(new java.awt.event.ActionListener() {
111            public void actionPerformed(java.awt.event.ActionEvent evt) {
112                jButtonYesActionPerformed(evt);
113            }
114        });
115
116        jButtonNo.setText("No");
117        jButtonNo.addActionListener(new java.awt.event.ActionListener() {
118            public void actionPerformed(java.awt.event.ActionEvent evt) {
119                jButtonNoActionPerformed(evt);
120            }
121        });
122
123        jCheckBoxApplyAll.setText("apply to all");
124
125        jLabelNumber.setText("#####");
126
127        jLabel3.setText("Number of files:");
128
129        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
130        getContentPane().setLayout(layout);
131        layout.setHorizontalGroup(
132            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
133            .add(layout.createSequentialGroup()
134                .addContainerGap()
135                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
136                    .add(jLabel1)
137                    .add(layout.createSequentialGroup()
138                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
139                        .add(9, 9, 9))
140                    .add(layout.createSequentialGroup()
141                        .add(jCheckBoxApplyAll)
142                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 138, Short.MAX_VALUE)
143                        .add(jButtonYes)
144                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
145                        .add(jButtonNo))
146                    .add(jLabel2)
147                    .add(layout.createSequentialGroup()
148                        .add(jLabel3)
149                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
150                        .add(jLabelNumber)))
151                .addContainerGap())
152        );
153        layout.setVerticalGroup(
154            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
155            .add(layout.createSequentialGroup()
156                .addContainerGap()
157                .add(jLabel1)
158                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
159                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
160                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
161                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
162                    .add(jLabel3)
163                    .add(jLabelNumber))
164                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
165                .add(jLabel2)
166                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
167                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
168                    .add(jCheckBoxApplyAll)
169                    .add(jButtonNo)
170                    .add(jButtonYes))
171                .addContainerGap())
172        );
173
174        pack();
175    }// </editor-fold>//GEN-END:initComponents
176
177    private void jButtonNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNoActionPerformed
178        DefaultListModel model = (DefaultListModel) jListIndexedFiles.getModel();
179
180        if(jCheckBoxApplyAll.isSelected()){
181             model.clear();
182            
183            jLabelNumber.setText("0");
184            this.setVisible(false);
185        }
186        else{
187            int[] indices = jListIndexedFiles.getSelectedIndices();
188
189            if(indices.length > 0){
190
191                for (int i = 0; i < indices.length; i++)
192                    model.remove(indices[i]);
193
194                //jListIndexedFiles.setModel(model);
195                jLabelNumber.setText(String.valueOf(Integer.valueOf(jLabelNumber.getText()) - indices.length));
196
197                if(model.isEmpty())
198                    this.setVisible(false);
199            }
200            else
201                JOptionPane.showMessageDialog(this, "You need to select some files.", "Select Files", JOptionPane.ERROR_MESSAGE);
202        }
203    }//GEN-LAST:event_jButtonNoActionPerformed
204
205    private void jButtonYesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonYesActionPerformed
206       boolean showTaskList = true;
207
208        DefaultListModel model = (DefaultListModel) jListIndexedFiles.getModel();
209        ArrayList<String> list = new ArrayList<String>();
210
211        if(jCheckBoxApplyAll.isSelected()){
212            Enumeration<String> en = (Enumeration<String>) model.elements();
213
214            while(en.hasMoreElements())
215                list.add(en.nextElement());
216
217            if(list.size() > 0)
218                AdminRefs.getInstance().reIndex(list);
219
220            model.clear();
221            
222            jLabelNumber.setText("0");
223            this.setVisible(false);
224        }
225        else{
226            int[] indices = jListIndexedFiles.getSelectedIndices();
227
228            if(indices.length > 0){
229
230                for (int i = 0; i < indices.length; i++){
231                    list.add((String) model.get(indices[i]));
232                    model.remove(indices[i]);
233                }
234
235                AdminRefs.getInstance().reIndex(list);
236                //jListIndexedFiles.setModel(model);
237                jLabelNumber.setText(String.valueOf(Integer.valueOf(jLabelNumber.getText()) - indices.length));
238
239                if(model.isEmpty())
240                    this.setVisible(false);
241            }
242            else{
243                JOptionPane.showMessageDialog(this, "You need to select some files.", "Select Files", JOptionPane.ERROR_MESSAGE);
244                showTaskList = false;
245            }
246        }
247
248        /*if (showTaskList)
249        {
250            TaskList tasks = TaskList.getInstance();
251            tasks.setVisible(true);
252            tasks.toFront();
253        }*/
254    }//GEN-LAST:event_jButtonYesActionPerformed
255
256    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
257        //if the user closes the window, none of the files will be reIndexed
258
259        jCheckBoxApplyAll.setSelected(true);
260        jButtonNo.doClick();
261    }//GEN-LAST:event_formWindowClosing
262
263    // Variables declaration - do not modify//GEN-BEGIN:variables
264    private javax.swing.JButton jButtonNo;
265    private javax.swing.JButton jButtonYes;
266    private javax.swing.JCheckBox jCheckBoxApplyAll;
267    private javax.swing.JLabel jLabel1;
268    private javax.swing.JLabel jLabel2;
269    private javax.swing.JLabel jLabel3;
270    private javax.swing.JLabel jLabelNumber;
271    private javax.swing.JList jListIndexedFiles;
272    private javax.swing.JScrollPane jScrollPane1;
273    // End of variables declaration//GEN-END:variables
274
275
276    /**
277     * Adds all the indexed files that can be re-indexed
278     *
279     * @param list of already indexed files
280     */
281    private synchronized void addAlreadyIndexedFiles(ArrayList<String> list){
282        DefaultListModel model = (DefaultListModel) jListIndexedFiles.getModel();
283
284        for(String filePath: list)
285            model.addElement(filePath);
286
287        //jListIndexedFiles.setModel(model);
288        jListIndexedFiles.revalidate();
289        this.repaint();
290        jLabelNumber.setText(String.valueOf(Integer.valueOf(jLabelNumber.getText()) + list.size()));
291    }
292
293    /**
294     * Get the list of already indexed files that can be re-indexed
295     *
296     * This list is accessible from the remote object PendingMessages, located on the server
297     */
298    public void getList(){
299        try {
300            ArrayList<String> list = pendingMessages.getFilesAlreadyIndexed();
301
302            if(list != null && list.size() > 0){
303                addAlreadyIndexedFiles(list);
304
305                if (this.isVisible() == false)
306                    this.setVisible(true);
307            }
308
309        } catch (RemoteException ex) {
310            LoggerFactory.getLogger(FileAlreadyIndexed.class).error(ex.getMessage(), ex);
311        }
312    }
313}