package realcix20.guis.views;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.Item;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.guis.views.settings.tools.GV;
import realcix20.guis.views.settings.tools.GVGroup;
import realcix20.guis.views.settings.tools.GVManager;
import realcix20.utils.GlobalValueManager;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
import com.jgoodies.uif_lite.panel.SimpleInternalFrame;
public class SettingsView extends JDialog implements ActionListener, TreeSelectionListener {
private MainView container;
private SimpleInternalFrame view;
private JTree tree;
private JComboBox languageChooser;
private JComboBox lookAndFeelsChooser;
private String stat;
private Vector gvGroups;
private GVGroup currentGVGroup;
private Vector groupComponents;
public void valueChanged(TreeSelectionEvent e) {
updateView();
}
public void actionPerformed(ActionEvent e) {
if ( e.getActionCommand().equals("Cancel") || e.getActionCommand().equals("Confirm") ) {
container.setEnabled(true);
dispose();
} else if (e.getActionCommand().equals("Restore Default")) {
restoreDefault();
} else if (e.getActionCommand().equals("Apply")) {
apply();
}
}
public SettingsView(MainView container) {
super(container);
this.container = container;
gvGroups = GVManager.generateGVGroups();
initMenuBar();
initComponents();
}
private void initMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.setVisible(true);
JMenu notVisibleMenu = new JMenu();
notVisibleMenu.setVisible(false);
menuBar.add(notVisibleMenu);
JMenuItem menuItem = new JMenuItem();
menuItem.setAccelerator(KeyStroke.getKeyStroke("ESCAPE"));
menuItem.setActionCommand("Cancel");
menuItem.addActionListener(this);
menuItem.setVisible(false);
notVisibleMenu.add(menuItem);
setJMenuBar(menuBar);
}
private void initComponents() {
JScrollPane scrollPane = new JScrollPane();
tree = new JTree();
view = new SimpleInternalFrame();
JPanel actionPanel = new JPanel();
JButton confirmButton = new JButton();
JButton cancelButton = new JButton();
//======== this ========
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
SettingsView thisWindow = (SettingsView)e.getSource();
thisWindow.getContainer().setEnabled(true);
thisWindow.dispose();
}
}
);
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 1.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 0.0, 1.0E-4};
//======== scrollPane ========
{
//---- tree ----
initSettingsTree();
tree.setPreferredSize(new Dimension(200, 68));
scrollPane.setViewportView(tree);
}
contentPane.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 10, 15, 15), 0, 0));
//======== view ========
{
view.setPreferredSize(new Dimension(450, 96));
tree.setSelectionRow(0);
}
contentPane.add(view, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(10, 10, 15, 10), 0, 0));
//======== actionPanel ========
{
actionPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//---- confirmButton ----
confirmButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.GLOBAL.CONFIRMBUTTON"), KeyEvent.VK_C));
confirmButton.setMnemonic(KeyEvent.VK_C);
confirmButton.setPreferredSize(new Dimension(100, 20));
confirmButton.setIcon(ImageManager.getImage(ImageManager.CONFIRM_IMAGE));
confirmButton.setActionCommand("Confirm");
confirmButton.addActionListener(this);
actionPanel.add(confirmButton);
//---- cancelButton ----
cancelButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.GLOBAL.CANCELBUTTON"), KeyEvent.VK_C));
cancelButton.setMnemonic(KeyEvent.VK_C);
cancelButton.setPreferredSize(new Dimension(100, 20));
cancelButton.setIcon(ImageManager.getImage(ImageManager.CANCEL_IMAGE));
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(this);
actionPanel.add(cancelButton);
}
contentPane.add(actionPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
pack();
setTitle(TxtManager.getTxt("SETTINGS.TITLE"));
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
private void initSettingsTree() {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
Iterator gvGroupsIter = gvGroups.iterator();
int n = 0;
while (gvGroupsIter.hasNext()) {
GVGroup gvGroup = (GVGroup)gvGroupsIter.next();
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(new ObjectInfo(-1,
TxtManager.getTxt("VIEW." + gvGroup.getGrp()), gvGroup.getGrp()));
rootNode.add(childNode);
if (n == 0) {
currentGVGroup = gvGroup;
}
n++;
}
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
tree.setModel(treeModel);
tree.setShowsRootHandles(true);
tree.setRootVisible(false);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setCellRenderer(new TreeCellRenderer());
tree.addTreeSelectionListener(this);
tree.updateUI();
}
class TreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,boolean selected,
boolean expanded,boolean leaf, int row, boolean hasFocus){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node.getUserObject() instanceof ObjectInfo) {
ObjectInfo oj = (ObjectInfo)node.getUserObject();
super.getTreeCellRendererComponent(tree, value, selected,expanded, leaf, row,hasFocus);
setIcon(null);
}
return this;
}
}
private void restoreDefault() {
Iterator groupComponentsIter = groupComponents.iterator();
Iterator gvsIter = currentGVGroup.getGvs().iterator();
while (gvsIter.hasNext()) {
GV gv = (GV)gvsIter.next();
JComponent component = (JComponent)groupComponentsIter.next();
if (component instanceof JComboBox) {
JComboBox comboBox = (JComboBox)component;
for (int i = 0; i < comboBox.getItemCount(); i++) {
Item item = (Item)comboBox.getItemAt(i);
if (item.getFactValue().equals(gv.getDefvalue())) {
comboBox.setSelectedIndex(i);
break;
}
}
}
}
}
private void apply() {
Iterator groupComponentsIter = groupComponents.iterator();
Iterator gvsIter = currentGVGroup.getGvs().iterator();
while (gvsIter.hasNext()) {
GV gv = (GV)gvsIter.next();
JComponent component = (JComponent)groupComponentsIter.next();
if (component instanceof JComboBox) {
JComboBox comboBox = (JComboBox)component;
Item item = (Item)comboBox.getSelectedItem();
GlobalValueManager.setValue(gv.getVid(), (String)item.getFactValue());
}
}
}
public void updateView() {
if (getTree().getLastSelectedPathComponent() != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)getTree().getLastSelectedPathComponent();
if (node.getUserObject() instanceof ObjectInfo) {
ObjectInfo oj = (ObjectInfo)node.getUserObject();
// System.err.println("oj.flag = " + oj.flag);
currentGVGroup = GVManager.findGVGroup(oj.flag, gvGroups);
updateRightView();
}
}
}
private void updateRightView() {
CellConstraints cc = new CellConstraints();
view.setTitle(TxtManager.getTxt("VIEW." + currentGVGroup.getGrp()));
Container viewContentPane = view.getContentPane();
viewContentPane.removeAll();
ColumnSpec[] columnSpec = new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("10px")
};
RowSpec[] rowSpec = new RowSpec[currentGVGroup.getGvs().size() * 2 + 3];
rowSpec[0] = new RowSpec("fill:10px");
rowSpec[1] = FormFactory.LINE_GAP_ROWSPEC;
int n = 1;
for (int i = 1; i <= currentGVGroup.getGvs().size(); i++) {
n++;
rowSpec[n] = FormFactory.DEFAULT_ROWSPEC;
n++;
rowSpec[n] = FormFactory.LINE_GAP_ROWSPEC;
}
rowSpec[n + 1] = new RowSpec(RowSpec.BOTTOM, Sizes.DEFAULT, FormSpec.DEFAULT_GROW);
viewContentPane.setLayout(new FormLayout(columnSpec, rowSpec));
n = 1;
groupComponents = new Vector();
for (int i = 1; i <= currentGVGroup.getGvs().size(); i++) {
GV gv = (GV)currentGVGroup.getGvs().get(i - 1);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(TxtManager.getTxt("VIEW.GV." + gv.getVid())));
panel.setLayout(new FormLayout(
new ColumnSpec[] {
new ColumnSpec("10px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("150px"),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec("150px")
},
RowSpec.decodeSpecs("5px, default, 5px")));
JLabel label = new JLabel(TxtManager.getTxt("VIEW.GV." + gv.getVid()));
panel.add(label, cc.xy(3, 2));
JComponent component = GVManager.getComponent(gv);
groupComponents.add(component);
panel.add(component, cc.xy(5, 2));
viewContentPane.add(panel, cc.xy(3, 3+(n-1)*2));
n++;
}
JPanel actionPanel = new JPanel();
actionPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton defaultButton = new JButton();
defaultButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.SETTINGSDIALOG.RESTOREDEFAULTBUTTON"), KeyEvent.VK_R));
defaultButton.setMnemonic(KeyEvent.VK_R);
defaultButton.setPreferredSize(new Dimension(125, 20));
defaultButton.setActionCommand("Restore Default");
defaultButton.addActionListener(this);
actionPanel.add(defaultButton);
JButton applyButton = new JButton();
applyButton.setText(MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.SETTINGSDIALOG.APPLYBUTTON"), KeyEvent.VK_A));
applyButton.setMnemonic(KeyEvent.VK_A);
applyButton.setPreferredSize(new Dimension(100, 20));
applyButton.setActionCommand("Apply");
applyButton.addActionListener(this);
actionPanel.add(applyButton);
viewContentPane.add(actionPanel, cc.xy(3, 3+(n-1)*2));
view.updateUI();
}
public MainView getContainer() {
return container;
}
public JTree getTree() {
return tree;
}
}