package org.ifmo.clgp.view;
import java.awt.Point;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.tree.*;
import org.ifmo.clgp.controller.ImageMetainfController;
import org.ifmo.clgp.controller.PopupActivationListener;
import org.ifmo.clgp.controller.PopupMenuActionListener;
import org.ifmo.clgp.controller.ViewButtonListener;
import org.ifmo.clgp.model.Image;
import org.ifmo.clgp.model.ImageNode;
import org.ifmo.clgp.model.StructureManager;
import org.ifmo.clgp.view.constants.MainWindowConstants;
import org.ifmo.clgp.view.tree.TreeCellRenderer;
/**
*
* @author Brantner
*/
public class View extends javax.swing.JFrame {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
/**
* Creates new form View
*/
public View() {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For
* details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
initComponents();
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
popup = new javax.swing.JPopupMenu();
createFolderMenuItem = new javax.swing.JMenuItem();
createImageMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
tree = new javax.swing.JTree();
jScrollPane2 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
descriptionArea = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
creationDateTextField = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
capacityTextField = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
operationSystemTextField = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel5 = new javax.swing.JLabel();
createFolderMenuItem.setText(MainWindowConstants.POPUP_MENU_ITEM_CREATE_FOLDER);
popup.add(createFolderMenuItem);
createImageMenuItem.setText(MainWindowConstants.POPUP_MENU_ITEM_CREATE_IMAGE);
popup.add(createImageMenuItem);
deleteMenuItem.setText(MainWindowConstants.POPUP_MENU_ITEM_DELETE);
popup.add(deleteMenuItem);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jSplitPane1.setDividerLocation(450);
jSplitPane1.setPreferredSize(new java.awt.Dimension(905, 450));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setModel(getTreeModel());
tree.setCellRenderer(new TreeCellRenderer());
jScrollPane1.setViewportView(tree);
jSplitPane1.setLeftComponent(jScrollPane1);
jScrollPane2.setPreferredSize(new java.awt.Dimension(450, 450));
jPanel1.setPreferredSize(new java.awt.Dimension(450, 450));
jLabel1.setText("Описание");
descriptionArea.setColumns(20);
descriptionArea.setRows(5);
jScrollPane3.setViewportView(descriptionArea);
jLabel2.setText("Дата создания");
creationDateTextField.setEditable(false);
jLabel3.setText("Объем");
capacityTextField.setEditable(false);
jLabel4.setText("Тип ОС");
operationSystemTextField.setEditable(false);
jButton1.setText("Отменить");
jButton2.setText("Сохранить");
jButton3.setText("Установить");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Название", "Объем", "Объем swap", "Скрипты"
}
));
jScrollPane4.setViewportView(jTable1);
jLabel5.setText("История");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(35, 35, 35)
.addComponent(jScrollPane3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(creationDateTextField)
.addComponent(capacityTextField)
.addComponent(operationSystemTextField)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(0, 420, Short.MAX_VALUE))
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(16, 16, 16)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(creationDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(capacityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(operationSystemTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton1))
.addContainerGap())
);
jScrollPane2.setViewportView(jPanel1);
jSplitPane1.setRightComponent(jScrollPane2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 950, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private MutableTreeNode rootNode = null;
private TreeModel getTreeModel() {
rootNode = buildTreeModel(StructureManager.getInstance().getRoot());
TreeModel treeModel = new DefaultTreeModel(rootNode);
return treeModel;
}
public void updateImageMetainf(Image image) {
if (image.getDescription() != null) {
descriptionArea.setText(image.getDescription());
}
capacityTextField.setText(image.getCapacity());
if (image.getCreationDate() != null) {
creationDateTextField.setText(df.format(image.getCreationDate()));
}
operationSystemTextField.setText(image.getOperationSystem());
}
public void removeTreeNode(MutableTreeNode node) {
((DefaultTreeModel) tree.getModel()).removeNodeFromParent(node);
}
public void updateTree(DefaultMutableTreeNode parent, Object child) {
((DefaultTreeModel) tree.getModel()).insertNodeInto(new DefaultMutableTreeNode(child), parent,
parent.getChildCount());
}
private DefaultMutableTreeNode buildTreeModel(ImageNode imageNode) {
DefaultMutableTreeNode result = new DefaultMutableTreeNode(imageNode);
for (Image img : imageNode.getImages()) {
result.add(new DefaultMutableTreeNode(img));
}
for (ImageNode i : imageNode.getNodes()) {
result.add(buildTreeModel(i));
}
return result;
}
public void addListeners(ImageMetainfController imc, PopupActivationListener pal,
PopupMenuActionListener pmal, ViewButtonListener vbl) {
tree.addTreeSelectionListener(imc);
tree.addMouseListener(pal);
createFolderMenuItem.addActionListener(pmal);
createImageMenuItem.addActionListener(pmal);
deleteMenuItem.addActionListener(pmal);
jButton1.addActionListener(vbl);
jButton2.addActionListener(vbl);
jButton3.addActionListener(vbl);
}
public void clearField() {
descriptionArea.setText(null);
creationDateTextField.setText(null);
capacityTextField.setText(null);
operationSystemTextField.setText(null);
}
public void activatePopup(Point p) {
TreePath path = tree.getPathForLocation(p.x, p.y);
if (path != null) {
tree.setSelectionPath(path);
Object o = ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject();
if (o instanceof ImageNode) {
popup.removeAll();
popup.add(createFolderMenuItem);
popup.add(createImageMenuItem);
popup.add(deleteMenuItem);
} else {
popup.removeAll();
popup.add(deleteMenuItem);
}
popup.show(tree, p.x, p.y);
}
}
public Image getSelectedImage() {
Image image = new Image();
image.setCapacity(capacityTextField.getText());
if (creationDateTextField.getText() != null && !creationDateTextField.getText().isEmpty()) {
try {
image.setCreationDate(df.parse(creationDateTextField.getText()));
} catch (ParseException ex) {
ex.printStackTrace();
}
} else {
image.setCreationDate(null);
}
image.setDescription(descriptionArea.getText());
image.setOperationSystem(operationSystemTextField.getText());
return image;
}
public void disableImageMetainf() {
clearField();
descriptionArea.setEditable(false);
creationDateTextField.setEditable(false);
capacityTextField.setEditable(false);
operationSystemTextField.setEditable(false);
}
public void enableImageMetainf() {
clearField();
descriptionArea.setEditable(true);
creationDateTextField.setEditable(true);
capacityTextField.setEditable(true);
operationSystemTextField.setEditable(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField capacityTextField;
private javax.swing.JMenuItem createFolderMenuItem;
private javax.swing.JMenuItem createImageMenuItem;
private javax.swing.JTextField creationDateTextField;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JTextArea descriptionArea;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField operationSystemTextField;
private javax.swing.JPopupMenu popup;
private javax.swing.JTree tree;
// End of variables declaration//GEN-END:variables
}