1 | package org.greenstone.gatherer.gui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.event.*;
|
---|
5 | import java.io.*;
|
---|
6 | import javax.swing.*;
|
---|
7 | import org.greenstone.gatherer.Dictionary;
|
---|
8 | import org.greenstone.gatherer.Gatherer;
|
---|
9 | import org.greenstone.gatherer.gui.TextFieldLabel;
|
---|
10 | import org.greenstone.gatherer.msm.MSMUtils;
|
---|
11 | import org.greenstone.gatherer.util.Utility;
|
---|
12 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
13 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
14 | import org.w3c.dom.*;
|
---|
15 |
|
---|
16 | public class LockFileDialog
|
---|
17 | extends ModalDialog {
|
---|
18 |
|
---|
19 | private int choice;
|
---|
20 | private Document document;
|
---|
21 | private JButton cancel_button;
|
---|
22 | private JButton ok_button;
|
---|
23 | private LockFileDialog self;
|
---|
24 |
|
---|
25 | static final public int NO_OPTION = 0;
|
---|
26 | static final public int YES_OPTION = 1;
|
---|
27 |
|
---|
28 | static final private Dimension LABEL_SIZE = new Dimension(100, 25);
|
---|
29 | static final private Dimension SIZE = new Dimension(500, 250);
|
---|
30 | static final private int ICON_SIZE = 30;
|
---|
31 |
|
---|
32 | public LockFileDialog(JFrame parent, String name, File lock_file) {
|
---|
33 | super(parent, get("Title"), true);
|
---|
34 | setSize(SIZE);
|
---|
35 | setJMenuBar(new SimpleMenuBar("2.3"));
|
---|
36 | this.self = this;
|
---|
37 | // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception.
|
---|
38 | document = Utility.parse(lock_file, false);
|
---|
39 | // Creation
|
---|
40 | JPanel content_pane = (JPanel) getContentPane();
|
---|
41 | JPanel upper_pane = new JPanel();
|
---|
42 | ImageIcon icon = Utility.getImage("lcolicn.gif");
|
---|
43 | ImageIcon scaled_icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_DEFAULT));
|
---|
44 | JLabel icon_label = new JLabel(scaled_icon);
|
---|
45 | JPanel title_pane = new JPanel();
|
---|
46 | JLabel title_one_label = new JLabel(get("General.Warning"));
|
---|
47 | JLabel title_two_label = new JLabel(get("Lockfile_Message_One"));
|
---|
48 | JPanel central_pane = new JPanel();
|
---|
49 | JPanel name_pane = new JPanel();
|
---|
50 | JLabel name_label = new JLabel(get("Name"));
|
---|
51 | name_label.setPreferredSize(LABEL_SIZE);
|
---|
52 | TextFieldLabel name_field = new TextFieldLabel(name);
|
---|
53 | JPanel person_pane = new JPanel();
|
---|
54 | JLabel person_label = new JLabel(get("User"));
|
---|
55 | person_label.setPreferredSize(LABEL_SIZE);
|
---|
56 | TextFieldLabel person_field = new TextFieldLabel(getValue("User"));
|
---|
57 | JPanel machine_pane = new JPanel();
|
---|
58 | JLabel machine_label = new JLabel(get("Machine"));
|
---|
59 | machine_label.setPreferredSize(LABEL_SIZE);
|
---|
60 | TextFieldLabel machine_field = new TextFieldLabel(getValue("Machine"));
|
---|
61 | JPanel created_pane = new JPanel();
|
---|
62 | JLabel created_label = new JLabel(get("Date"));
|
---|
63 | created_label.setPreferredSize(LABEL_SIZE);
|
---|
64 | TextFieldLabel created_field = new TextFieldLabel(getValue("Date"));
|
---|
65 | JLabel central_label = new JLabel(get("Lockfile_Message_Two"));
|
---|
66 | JPanel button_pane = new JPanel();
|
---|
67 | ok_button = new JButton(get("General.OK"));
|
---|
68 | cancel_button = new JButton(get("General.Cancel"));
|
---|
69 | // Connection
|
---|
70 | ok_button.addActionListener(new MyActionListener());
|
---|
71 | cancel_button.addActionListener(new MyActionListener());
|
---|
72 | // Layout
|
---|
73 | icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
|
---|
74 |
|
---|
75 | title_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
76 | title_pane.add(title_one_label);
|
---|
77 | title_pane.add(title_two_label);
|
---|
78 |
|
---|
79 | upper_pane.setLayout(new BorderLayout());
|
---|
80 | upper_pane.add(icon_label, BorderLayout.WEST);
|
---|
81 | upper_pane.add(title_pane, BorderLayout.CENTER);
|
---|
82 |
|
---|
83 | name_pane.setLayout(new BorderLayout());
|
---|
84 | name_pane.add(name_label, BorderLayout.WEST);
|
---|
85 | name_pane.add(name_field, BorderLayout.CENTER);
|
---|
86 |
|
---|
87 | person_pane.setLayout(new BorderLayout());
|
---|
88 | person_pane.add(person_label, BorderLayout.WEST);
|
---|
89 | person_pane.add(person_field, BorderLayout.CENTER);
|
---|
90 |
|
---|
91 | machine_pane.setLayout(new BorderLayout());
|
---|
92 | machine_pane.add(machine_label, BorderLayout.WEST);
|
---|
93 | machine_pane.add(machine_field, BorderLayout.CENTER);
|
---|
94 |
|
---|
95 | created_pane.setLayout(new BorderLayout());
|
---|
96 | created_pane.add(created_label, BorderLayout.WEST);
|
---|
97 | created_pane.add(created_field, BorderLayout.CENTER);
|
---|
98 |
|
---|
99 | central_pane.setLayout(new GridLayout(5,1,0,5));
|
---|
100 | central_pane.add(name_pane);
|
---|
101 | central_pane.add(person_pane);
|
---|
102 | central_pane.add(machine_pane);
|
---|
103 | central_pane.add(created_pane);
|
---|
104 | central_pane.add(central_label);
|
---|
105 |
|
---|
106 | button_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
107 | button_pane.add(ok_button);
|
---|
108 | button_pane.add(cancel_button);
|
---|
109 |
|
---|
110 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
111 | content_pane.setLayout(new BorderLayout());
|
---|
112 | content_pane.add(upper_pane, BorderLayout.NORTH);
|
---|
113 | content_pane.add(central_pane, BorderLayout.CENTER);
|
---|
114 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
115 | // Display
|
---|
116 | if(parent != null) {
|
---|
117 | Rectangle frame_bounds = parent.getBounds();
|
---|
118 | setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
122 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public int getChoice() {
|
---|
127 | setVisible(true);
|
---|
128 | return choice;
|
---|
129 | }
|
---|
130 |
|
---|
131 | private String getValue(String name) {
|
---|
132 | String value = "";
|
---|
133 | if(document != null) {
|
---|
134 | try {
|
---|
135 | Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name);
|
---|
136 | value = MSMUtils.getValue(value_element);
|
---|
137 | }
|
---|
138 | catch (Exception error) {
|
---|
139 | }
|
---|
140 | }
|
---|
141 | else {
|
---|
142 | value = get("Error");
|
---|
143 | }
|
---|
144 | return value;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private class MyActionListener
|
---|
148 | implements ActionListener {
|
---|
149 | public void actionPerformed(ActionEvent event) {
|
---|
150 | if(event.getSource() == ok_button) {
|
---|
151 | choice = YES_OPTION;
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | choice = NO_OPTION;
|
---|
155 | }
|
---|
156 | self.setVisible(false);
|
---|
157 | self.dispose();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | static public void main(String[] args) {
|
---|
162 | JFrame frame = new JFrame();
|
---|
163 | frame.setSize(640,480);
|
---|
164 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
165 | frame.show();
|
---|
166 | LockFileDialog dialog = new LockFileDialog(frame, "Test", new File(args[0]));
|
---|
167 | int choice = dialog.getChoice();
|
---|
168 | }
|
---|
169 |
|
---|
170 | static private String get(String key) {
|
---|
171 | if(key.indexOf(".") == -1) {
|
---|
172 | key = "LockFileDialog." + key;
|
---|
173 | }
|
---|
174 | return Gatherer.dictionary.get(key);
|
---|
175 | }
|
---|
176 | }
|
---|