Projet

Général

Profil

Télécharger (9,11 ko) Statistiques
| Branche: | Tag: | Révision:
/*
* This program is a part of the IoTa project.
*
* Copyright © 2008-2012 Université de Caen Basse-Normandie, GREYC
*
* 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 3 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.
* <http://www.gnu.org/licenses/>
*
* See AUTHORS for a list of contributors.
*/
package fr.unicaen.iota.validator.gui;

import fr.unicaen.iota.validator.Configuration;
import fr.unicaen.iota.validator.IOTA;
import fr.unicaen.iota.validator.model.EPCISLink;
import java.awt.Frame;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

/**
*
*/
public class Preferences extends JDialog {

private static final Log log = LogFactory.getLog(Preferences.class);
private IOTA iota;

/** Creates new Preferences form */
public Preferences(Frame parent, boolean modal, IOTA iota) {
super(parent, modal);
this.iota = iota;
initComponents();
createTree();
setLocationRelativeTo(parent);
}

void updateTreeModel() {
jTree1.updateUI();
}

private void createTree() {
jTree1.setEditable(true);
TreeRendererPreferences trp = new TreeRendererPreferences();
jTree1.setCellRenderer(trp);
jTree1.setCellEditor(new TreeEditorPreferences(jTree1, trp));
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Business Location");
((DefaultTreeModel) (jTree1.getModel())).setRoot(root);
createNodes(root);
jTree1.setRootVisible(false);
jTree1.updateUI();
}

private void createNodes(DefaultMutableTreeNode top) {
for (String bizLoc : iota.keySet()) {
EPCISLink epcisLink = iota.get(bizLoc);
PreferencesLeaf pLeaf1 = new PreferencesLeaf(epcisLink, PreferencesLeaf.Type.EPCIS, this);
PreferencesLeaf pLeaf2 = new PreferencesLeaf(epcisLink.getDSLink(), PreferencesLeaf.Type.DS, this);
List<PreferencesLeaf> pLeafList = new ArrayList<PreferencesLeaf>();
pLeafList.add(pLeaf1);
pLeafList.add(pLeaf2);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new PreferencesNode(bizLoc, pLeafList));
node.add(new DefaultMutableTreeNode(pLeaf1));
node.add(new DefaultMutableTreeNode(pLeaf2));
top.add(node);
}
}

/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();

setBackground(new java.awt.Color(239, 235, 231));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});

jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

jLabel1.setText(" add or remove servers during analysis:");
jLabel1.setPreferredSize(new java.awt.Dimension(248, 30));
jPanel1.add(jLabel1);

add(jPanel1, java.awt.BorderLayout.NORTH);

jButton2.setText("Validate");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel2.add(jButton2);

jButton1.setText("load");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);

jButton3.setText("save");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel2.add(jButton3);

add(jPanel2, java.awt.BorderLayout.SOUTH);

jScrollPane1.setViewportView(jTree1);

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
);

add(jPanel3, java.awt.BorderLayout.CENTER);

pack();
}// </editor-fold>//GEN-END:initComponents

/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
closeDialog(null);
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
if (f.exists()) {
int value = JOptionPane.showConfirmDialog(this, "This file allready exist.\n Do you really want to erase it?");
if (value != JOptionPane.OK_OPTION) {
return;
}
}
saveIOTA(f.getAbsolutePath());
}

}//GEN-LAST:event_jButton3ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
int value = JOptionPane.showConfirmDialog(this, "Do you really want load the new platform schema?");
if (value != JOptionPane.OK_OPTION) {
return;
}
iota.clear();
Configuration.IOTA_XML_SCHEMA = f.getAbsolutePath();
try {
iota.loadFromXML();
} catch (SAXException ex) {
log.error(null, ex);
JOptionPane.showMessageDialog(this, "A problem occurred during parsing!");
return;
} catch (IOException ex) {
log.error(null, ex);
JOptionPane.showMessageDialog(this, "Anable to open or read the file!");
return;
}
createTree();
}
}//GEN-LAST:event_jButton1ActionPerformed

private void saveIOTA(String path) {
FileWriter fw = null;
try {
fw = new FileWriter(path);
fw.write(iota.toXML());
fw.close();
} catch (IOException ex) {
log.error(null, ex);
} finally {
try {
fw.close();
} catch (IOException ex) {
log.error(null, ex);
}

}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration//GEN-END:variables
}
(18-18/30)