
org.tango.pogo.gui.TemplateChooser Maven / Gradle / Ivy
//+======================================================================
// $Source: $
//
// Project: Tango
//
// Description: Basic Dialog Class to display info
//
// $Author: pascal_verdier $
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2009,2010,2011,2012,2013,2014,2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango 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 3 of the License, or
// (at your option) any later version.
//
// Tango 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 Tango. If not, see .
//
// $Revision: $
//
// $Log: $
//
//-======================================================================
package org.tango.pogo.gui;
import org.tango.pogo.pogoDsl.ClassDescription;
import fr.esrf.tangoatk.widget.util.ATKGraphicsUtils;
import org.tango.pogo.gui.tools.PogoException;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//===============================================================
/**
* JDialog Class to display info
*
* @author Pascal Verdier
*/
//===============================================================
@SuppressWarnings({"MagicConstant", "Convert2Diamond"})
public class TemplateChooser extends JDialog {
private JFrame parent;
private DeviceClass deviceClass = null;
private int returnValue = JOptionPane.OK_OPTION;
private static final String extension = ".xmi";
//===============================================================
/**
* Creates new form TemplateChooser
*/
//===============================================================
public TemplateChooser(JFrame parent) throws PogoException {
super(parent, true);
this.parent = parent;
initComponents();
List templates = getTemplates();
for (TemplateClass template : templates) {
comboBox.addItem(template);
}
titleLabel.setText("Select a TANGO class template");
pack();
ATKGraphicsUtils.centerDialog(this);
}
//===============================================================
//===============================================================
private static String getPath() {
return System.getenv("TEMPLATES_PATH");
}
//===============================================================
//===============================================================
public static boolean templatesAvailable() {
String path = getPath();
if (path==null)
return false;
File file = new File(path);
String[] files = file.list();
return files!=null && files.length>0;
}
//===============================================================
//===============================================================
private List getTemplates() throws PogoException {
// Get custom templates if defined
List list = new ArrayList<>();
String path = getPath();
if (path!=null) {
List customList = getTemplateClasses(path);
for (TemplateClass templateClass : customList)
list.add(templateClass);
}
Collections.sort(list, new TemplateCompare());
return list;
}
//===============================================================
//===============================================================
private List getTemplateClasses(String path) {
List list = new ArrayList<>();
// Get file list
File file = new File(path);
String[] files = file.list();
if (files!=null) {
// And build template list from xmi files
for (String fileName : files) {
if (new File(path+'/'+fileName).isFile())
if (fileName.endsWith(extension))
list.add(new TemplateClass(path, fileName));
}
}
return list;
}
//===============================================================
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//===============================================================
// //GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
javax.swing.JPanel topPanel = new javax.swing.JPanel();
titleLabel = new javax.swing.JLabel();
javax.swing.JPanel centerPanel = new javax.swing.JPanel();
comboBox = new javax.swing.JComboBox();
javax.swing.JButton helpBtn = new javax.swing.JButton();
javax.swing.JPanel bottomPanel = new javax.swing.JPanel();
javax.swing.JButton okBtn = new javax.swing.JButton();
javax.swing.JButton cancelBtn = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
titleLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
titleLabel.setText("Dialog Title");
topPanel.add(titleLabel);
getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);
centerPanel.setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
centerPanel.add(comboBox, gridBagConstraints);
helpBtn.setText("?");
helpBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
helpBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
centerPanel.add(helpBtn, gridBagConstraints);
getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);
okBtn.setText("OK");
okBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okBtnActionPerformed(evt);
}
});
bottomPanel.add(okBtn);
cancelBtn.setText("Cancel");
cancelBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelBtnActionPerformed(evt);
}
});
bottomPanel.add(cancelBtn);
getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH);
pack();
}// //GEN-END:initComponents
//===============================================================
//===============================================================
@SuppressWarnings("UnusedParameters")
private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
try {
TemplateClass templateClass = (TemplateClass) comboBox.getSelectedItem();
deviceClass = new DeviceClass(templateClass.fileName);
// Remove info from ID
ClassDescription classDescription = deviceClass.getPogoDeviceClass().getDescription();
classDescription.setSourcePath("");
classDescription.setCopyright("");
classDescription.getIdentification().setAuthor("");
classDescription.getIdentification().setBus("");
classDescription.getIdentification().setContact("");
classDescription.getIdentification().setEmailDomain("");
ClassDialog classDialog = new ClassDialog(parent, null, deviceClass, false);
if (classDialog.showDialog()==JOptionPane.OK_OPTION) {
deviceClass = classDialog.getInputs();
returnValue = JOptionPane.OK_OPTION;
}
else
returnValue = JOptionPane.CANCEL_OPTION;
}
catch (PogoException e) {
e.popup(this);
}
doClose();
}//GEN-LAST:event_okBtnActionPerformed
//===============================================================
//===============================================================
@SuppressWarnings("UnusedParameters")
private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
returnValue = JOptionPane.CANCEL_OPTION;
doClose();
}//GEN-LAST:event_cancelBtnActionPerformed
//===============================================================
//===============================================================
@SuppressWarnings("UnusedParameters")
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
returnValue = JOptionPane.CANCEL_OPTION;
doClose();
}//GEN-LAST:event_closeDialog
//===============================================================
//===============================================================
@SuppressWarnings("UnusedParameters")
private void helpBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpBtnActionPerformed
// TODO add your handling code here:
JOptionPane.showMessageDialog(this, "To add your own templates:\n" +
" - Add xmi files in a specific directory\n" +
" - Export TEMPLATES_PATH environment variable with path of this directory.\n" +
" - Restart Pogo.\n\n" +
"Your templates could override the default ones");
}//GEN-LAST:event_helpBtnActionPerformed
//===============================================================
//===============================================================
private void doClose() {
if (parent==null)
System.exit(0);
else {
setVisible(false);
dispose();
}
}
//===============================================================
//===============================================================
public DeviceClass getDeviceClass() throws PogoException {
return deviceClass;
}
//===============================================================
//===============================================================
public int showDialog() {
setVisible(true);
return returnValue;
}
//===============================================================
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox comboBox;
private javax.swing.JLabel titleLabel;
// End of variables declaration//GEN-END:variables
//===============================================================
//===============================================================
/**
* @param args the command line arguments
*/
//===============================================================
public static void main(String args[]) {
try {
new TemplateChooser(null).setVisible(true);
}
catch (PogoException e) {
e.popup(new JFrame());
System.exit(0);
}
}
//===============================================================
//===============================================================
//===============================================================
//===============================================================
private class TemplateClass {
private String name;
private String fileName;
//===========================================================
private TemplateClass(String path, String fileName) {
this.fileName = path + '/' + fileName;
this.name = fileName.substring(0, fileName.length()-extension.length());
}
//===========================================================
public String toString() {
return name;// + "\t(" + fileName + ")";
}
//===========================================================
}
//======================================================
/**
* Template class to sort collection
*/
//======================================================
class TemplateCompare implements Comparator {
public int compare(TemplateClass t1, TemplateClass t2) {
return t1.name.compareTo(t2.name);
}
}
//===============================================================
//===============================================================
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy