Newer
Older
* This file is part of Artemis
*
* Copyright(C) 2012 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package uk.ac.sanger.artemis.components;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;
import java.util.Map.Entry;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import uk.ac.sanger.artemis.components.genebuilder.TextAreaDocumentListener;
import uk.ac.sanger.artemis.util.Document;
import uk.ac.sanger.artemis.util.FileDocument;
/**
* Project file management system using a properties file.
*
* Example of the syntax for defining a project in the property file:
* project.Pknowlsei.ref = Pknowlsei:Pk_strainH_chr01
* project.Pknowlsei.chado = genedb-db.sanger.ac.uk:5432/snapshot?genedb_ro
* project.Pknowlsei.title = Pknowlsei
*/
public class ProjectProperty extends JFrame
{
private static final long serialVersionUID = 1L;
private HashMap<String, HashMap<String, String>> projects;
private final static int REFERENCE = 1;
private final static int ANNOTATION = 2;
private final static int NEXT_GEN_DATA = 3;
private final static int CHADO = 4;
private static org.apache.log4j.Logger logger4j =
org.apache.log4j.Logger.getLogger(ProjectProperty.class);
private final static String[] TYPES =
{ "title", "ref", "annotation", "bam", "vcf", "chado" };
this.getClass().getClassLoader().getResourceAsStream("etc/project.properties");
try
{
logger4j.debug("Reading properties from: "+
this.getClass().getClassLoader().getResource("etc/project.properties").toURI());
}
catch (URISyntaxException e1){}
final Properties projectProps = new Properties();
try
{
projectProps.load(ins);
ins.close();
}
catch (IOException e)
{
e.printStackTrace();
}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
final String [] propFiles =
{
"project.properties",
System.getProperty("user.home") + File.separator + ".project.properties"
};
for(int i=0; i<propFiles.length; i++)
{
final Document doc =
new FileDocument(new File(propFiles[i]));
if(doc.readable())
{
try
{
ins = doc.getInputStream();
projectProps.load(ins);
}
catch (IOException e)
{
e.printStackTrace();
}
logger4j.debug("Reading properties from: "+propFiles[i]);
}
}
projects = getProjectMap(projectProps);
createProjectViewer((JPanel) getContentPane());
pack();
setVisible(true);
}
private void createProjectViewer(JPanel panel)
{
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
final DefaultListModel model = new DefaultListModel();
final JList projectList = new JList(model);
final JScrollPane jspList = new JScrollPane(projectList);
Object[] items = projects.keySet().toArray();
for (int i=0; i<items.length; i++)
model.add(i, items[i]);
final Box yBox = Box.createVerticalBox();
final LaunchActionListener listener = new LaunchActionListener();
projectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
projectList.setVisibleRowCount(-1);
panel.add(jspList, BorderLayout.WEST);
panel.add(yBox, BorderLayout.CENTER);
// menus
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
final JMenuItem exitMenu = new JMenuItem("Exit");
fileMenu.add(exitMenu);
exitMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
int status = JOptionPane.showConfirmDialog(
ProjectProperty.this, "Exit?", "Exit", JOptionPane.YES_NO_OPTION);
if(status == JOptionPane.YES_OPTION)
Splash.exitApp();
}
});
final JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
final JMenuItem addProject = new JMenuItem("Add Project");
addProject.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
String projName =
JOptionPane.showInputDialog(ProjectProperty.this,
"Project Name", "New Project", JOptionPane.QUESTION_MESSAGE);
if(projName == null)
return;
projects.put(projName, new HashMap<String, String>());
model.add(model.getSize(), projName);
projectList.repaint();
}
});
editMenu.add(addProject);
final JMenuItem removeProject = new JMenuItem("Remove Project");
removeProject.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
projects.remove(projectList.getSelectedValue());
model.remove(projectList.getSelectedIndex());
projectList.repaint();
yBox.removeAll();
yBox.repaint();
}
});
editMenu.add(removeProject);
//
openArt.addActionListener(listener);
panel.add(openArt, BorderLayout.SOUTH);
final GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.VERTICAL;
c.ipadx = 10;
c.ipady = 10;
projectList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
if(e.getValueIsAdjusting() == false &&
projectList.getSelectedIndex() > -1)
refreshProperties(projectList, yBox, listener);
}
});
}
/**
* Refresh components in the properties panel.
* @param projectList
* @param yBox
* @param listener
*/
private void refreshProperties(final JList projectList,
final Box yBox,
final LaunchActionListener listener)
{
yBox.removeAll();
final HashMap<String, String> projProps = projects.get(projectList.getSelectedValue());
final HashMap<Integer, QualifierTextArea> settings = new HashMap<Integer, QualifierTextArea>();
for(final String key: projProps.keySet())
{
Box xBox = Box.createHorizontalBox();
final QualifierTextArea qta = new QualifierTextArea();
Border loweredbevel = BorderFactory.createLoweredBevelBorder();
TitledBorder title = BorderFactory.createTitledBorder(
loweredbevel, key);
title.setTitlePosition(TitledBorder.ABOVE_TOP);
qta.setBorder(title);
qta.setText(projProps.get(key));
qta.getDocument().addDocumentListener(
new TextAreaDocumentListener(qta));
xBox.add(qta);
// REMOVE PROPERTY
final JButton removeProperty = new JButton("X");
removeProperty.setOpaque(false);
Font font = removeProperty.getFont().deriveFont(Font.BOLD);
removeProperty.setFont(font);
removeProperty.setToolTipText("REMOVE");
removeProperty.setForeground(new Color(139, 35, 35));
removeProperty.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
projProps.remove(key);
refreshProperties(projectList, yBox, listener);
}
});
xBox.add(removeProperty);
//
yBox.add(xBox);
if(key.equals("ref"))
settings.put(ProjectProperty.REFERENCE, qta);
else if(key.equals("annotation"))
settings.put(ProjectProperty.ANNOTATION, qta);
else if(key.equals("bam") || key.equals("vcf") || key.equals("bcf"))
settings.put(ProjectProperty.NEXT_GEN_DATA, qta);
else if(key.equals("chado"))
settings.put(ProjectProperty.CHADO, qta);
}
// ADD property
Box xBox = Box.createHorizontalBox();
final JButton addPropertyButton = new JButton("NEW PROPERTY");
final JComboBox propertyList = new JComboBox(TYPES);
xBox.add(addPropertyButton);
xBox.add(propertyList);
xBox.add(Box.createHorizontalGlue());
yBox.add(xBox);
addPropertyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
String key = (String) propertyList.getSelectedItem();
if(!projProps.containsKey(key))
{
projProps.put(key, "");
refreshProperties(projectList, yBox, listener);
//
yBox.add(Box.createVerticalGlue());
yBox.revalidate();
yBox.repaint();
listener.setSettings(settings);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* Create a project hash of the properties.
* @param projectProps
* @return
*/
private HashMap<String, HashMap<String, String>> getProjectMap(final Properties projectProps)
{
final HashMap<String, HashMap<String, String>> projects =
new HashMap<String, HashMap<String, String>>();
for (Entry<Object, Object> propItem : projectProps.entrySet())
{
String key = (String) propItem.getKey();
String value = (String) propItem.getValue();
if (key.startsWith("project."))
{
key = key.substring(8);
int ind = key.indexOf(".");
if (ind > -1)
{
String projName = key.substring(0, ind);
key = key.substring(ind + 1);
final HashMap<String, String> thisProj;
if (projects.containsKey(projName))
thisProj = projects.get(projName);
else
thisProj = new HashMap<String, String>();
thisProj.put(key, value);
projects.put(projName, thisProj);
}
}
}
return projects;
}
class LaunchActionListener implements ActionListener
{
private String args[] = new String[]{};
private void setSettings(HashMap<Integer, QualifierTextArea> settings)
{
try
{
System.getProperties().remove("bam");
System.getProperties().remove("chado");
}
catch(Exception e){ e.printStackTrace(); }
final Set<Integer> keys = settings.keySet();
final Vector<String> vargs = new Vector<String>();
final Vector<String> vann = new Vector<String>();
for(Integer key: keys)
{
switch(key)
{
case ProjectProperty.REFERENCE:
vargs.add( settings.get(key).getText() );
break;
case ProjectProperty.ANNOTATION:
vann.add( settings.get(key).getText() );
break;
case ProjectProperty.NEXT_GEN_DATA:
System.setProperty("bam", settings.get(key).getText().replaceAll("\\s+", ","));
break;
case ProjectProperty.CHADO:
System.setProperty("chado", settings.get(key).getText().trim());
break;
}
}
args = new String[vargs.size()+(vann.size()*2)];
for(int i=0; i<vargs.size(); i++)
args[i] = vargs.get(i);
for(int i=0; i<vann.size(); i++)
{
args[vargs.size()+(i*2)] = "+";
args[vargs.size()+(i*2)+1] = vann.get(i);
}
}
public void actionPerformed(ActionEvent arg0)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
final ArtemisMain main_window = new ArtemisMain(args);
main_window.setVisible(true);
main_window.readArgsAndOptions(args);
}
});
}
}
public static void main(String args[])
{
new ProjectProperty();
}
}