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 */
019/*
020 * To change this template, choose Tools | Templates
021 * and open the template in the editor.
022 */
023
024/*
025 * QueryHistory.java
026 *
027 * Created on 21/Jul/2010, 0:04:27
028 */
029
030package pt.ua.dicoogle.rGUI.client.windows;
031
032import java.awt.Image;
033import java.awt.Toolkit;
034import java.util.Iterator;
035import java.util.Observable;
036import java.util.Observer;
037import java.util.concurrent.Semaphore;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042import javax.swing.DefaultListModel;
043import javax.swing.JCheckBox;
044import javax.swing.JOptionPane;
045import javax.swing.JTextField;
046import pt.ua.dicoogle.Main;
047import pt.ua.dicoogle.core.QueryHistoryEntry;
048import pt.ua.dicoogle.core.QueryHistorySupport;
049
050/**
051 *
052 * @author samuelcampos
053 */
054@Deprecated
055public class QueryHistory extends javax.swing.JFrame implements Observer {
056    private QueryHistorySupport QHS = QueryHistorySupport.getInstance();
057    private JTextField queryField = null;
058    private JCheckBox jCheckBoxKeywords = null;
059
060    private static QueryHistory instance = null;
061    private static Semaphore sem = new Semaphore(1, true);
062
063    public static synchronized QueryHistory getInstance()
064    {
065        try
066        {
067            sem.acquire();
068            if (instance == null)
069            {
070                instance = new QueryHistory();
071            }
072            sem.release();
073        }
074        catch (InterruptedException ex)
075        {
076            LoggerFactory.getLogger(QueryHistory.class).error(ex.getMessage(), ex);
077        }
078        return instance;
079    }
080
081    /** Creates new form QueryHistory */
082    private QueryHistory() {
083        initComponents();
084
085        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
086        this.setIconImage(image);
087
088        QHS.addObserver(this);
089        
090        updateList();
091    }
092
093    /** This method is called from within the constructor to
094     * initialize the form.
095     * WARNING: Do NOT modify this code. The content of this method is
096     * always regenerated by the Form Editor.
097     */
098    @SuppressWarnings("unchecked")
099    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
100    private void initComponents() {
101
102        jScrollPane1 = new javax.swing.JScrollPane();
103        jListQueryHistory = new javax.swing.JList();
104        jButtonClose = new javax.swing.JButton();
105        jButtonInsert = new javax.swing.JButton();
106        jButtonDeleteThis = new javax.swing.JButton();
107        jButtonDeleteAll = new javax.swing.JButton();
108        jLabel2 = new javax.swing.JLabel();
109
110        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
111        setTitle("Query History");
112        setMinimumSize(new java.awt.Dimension(490, 270));
113
114        jListQueryHistory.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
115        jListQueryHistory.addMouseListener(new java.awt.event.MouseAdapter() {
116            public void mouseClicked(java.awt.event.MouseEvent evt) {
117                jListQueryHistoryMouseClicked(evt);
118            }
119        });
120        jScrollPane1.setViewportView(jListQueryHistory);
121
122        jButtonClose.setText("Close");
123        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
124            public void actionPerformed(java.awt.event.ActionEvent evt) {
125                jButtonCloseActionPerformed(evt);
126            }
127        });
128
129        jButtonInsert.setText("Insert into Query");
130        jButtonInsert.addActionListener(new java.awt.event.ActionListener() {
131            public void actionPerformed(java.awt.event.ActionEvent evt) {
132                jButtonInsertActionPerformed(evt);
133            }
134        });
135
136        jButtonDeleteThis.setText("Delete Query");
137        jButtonDeleteThis.addActionListener(new java.awt.event.ActionListener() {
138            public void actionPerformed(java.awt.event.ActionEvent evt) {
139                jButtonDeleteThisActionPerformed(evt);
140            }
141        });
142
143        jButtonDeleteAll.setText("Delete All");
144        jButtonDeleteAll.addActionListener(new java.awt.event.ActionListener() {
145            public void actionPerformed(java.awt.event.ActionEvent evt) {
146                jButtonDeleteAllActionPerformed(evt);
147            }
148        });
149
150        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
151        jLabel2.setText("Query History:");
152
153        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
154        getContentPane().setLayout(layout);
155        layout.setHorizontalGroup(
156            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
157            .add(layout.createSequentialGroup()
158                .addContainerGap()
159                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
160                    .add(jLabel2)
161                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
162                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
163                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
164                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
165                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
166                                .add(jButtonDeleteAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
167                                .add(jButtonDeleteThis, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
168                                .add(jButtonInsert, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
169                            .add(jButtonClose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
170                .addContainerGap())
171        );
172        layout.setVerticalGroup(
173            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
174            .add(layout.createSequentialGroup()
175                .add(20, 20, 20)
176                .add(jLabel2)
177                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
178                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
179                    .add(layout.createSequentialGroup()
180                        .add(jButtonInsert)
181                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
182                        .add(jButtonDeleteThis)
183                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
184                        .add(jButtonDeleteAll)
185                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 84, Short.MAX_VALUE)
186                        .add(jButtonClose))
187                    .add(layout.createSequentialGroup()
188                        .add(1, 1, 1)
189                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)))
190                .addContainerGap())
191        );
192
193        pack();
194    }// </editor-fold>//GEN-END:initComponents
195
196    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
197        this.setVisible(false);
198        MainWindow.getInstance().toFront();
199        this.dispose();
200    }//GEN-LAST:event_jButtonCloseActionPerformed
201
202    private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInsertActionPerformed
203        QueryHistoryEntry<String, Boolean> query = (QueryHistoryEntry<String, Boolean>) jListQueryHistory.getSelectedValue() ;
204        if (query==null || query.getKey()==null)
205        {
206
207            JOptionPane.showMessageDialog(this, "Select a valid query.", "Missing query", JOptionPane.INFORMATION_MESSAGE);
208            return ;
209        }
210        queryField.setText(query.getKey());
211        jCheckBoxKeywords.setSelected(query.getValue());
212    }//GEN-LAST:event_jButtonInsertActionPerformed
213
214    private void jButtonDeleteAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteAllActionPerformed
215        QHS.deleteAll();
216    }//GEN-LAST:event_jButtonDeleteAllActionPerformed
217
218    private void jButtonDeleteThisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteThisActionPerformed
219        QueryHistoryEntry<String, Boolean> query = (QueryHistoryEntry<String, Boolean>) jListQueryHistory.getSelectedValue() ;
220        if(query == null)
221            JOptionPane.showMessageDialog(this, "Please select one query to delete.", "Delete Query", JOptionPane.INFORMATION_MESSAGE);
222        else
223            QHS.deleteQuery(query);
224    }//GEN-LAST:event_jButtonDeleteThisActionPerformed
225
226    private void jListQueryHistoryMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListQueryHistoryMouseClicked
227        if (evt.getClickCount() == 2)
228              jButtonInsert.doClick();
229    }//GEN-LAST:event_jListQueryHistoryMouseClicked
230
231    // Variables declaration - do not modify//GEN-BEGIN:variables
232    private javax.swing.JButton jButtonClose;
233    private javax.swing.JButton jButtonDeleteAll;
234    private javax.swing.JButton jButtonDeleteThis;
235    private javax.swing.JButton jButtonInsert;
236    private javax.swing.JLabel jLabel2;
237    private javax.swing.JList jListQueryHistory;
238    private javax.swing.JScrollPane jScrollPane1;
239    // End of variables declaration//GEN-END:variables
240
241
242     @Override
243    public void update(Observable o, Object arg) {
244        updateList();
245    }
246
247    private void updateList(){
248        Iterator<QueryHistoryEntry<String, Boolean>> it = QHS.getQueryHistory();
249
250        DefaultListModel listModel = new DefaultListModel();
251
252        while(it.hasNext())
253        {
254            listModel.addElement(it.next());
255        }
256        jListQueryHistory.setModel(listModel);
257    }
258
259    public void setJTextFieldQuery(JTextField queryField, JCheckBox jCheckBoxKeywords){
260        this.queryField = queryField;
261        this.jCheckBoxKeywords = jCheckBoxKeywords;
262    }
263}