source: trunk/gli/src/org/greenstone/gatherer/gui/LockFileDialog.java@ 4659

Last change on this file since 4659 was 4459, checked in by jmt12, 21 years ago

2030100: With the bug in ModalDialog fixed I can revert this back.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1package org.greenstone.gatherer.gui;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.io.*;
6import javax.swing.*;
7import org.greenstone.gatherer.Dictionary;
8import org.greenstone.gatherer.Gatherer;
9import org.greenstone.gatherer.gui.TextFieldLabel;
10import org.greenstone.gatherer.msm.MSMUtils;
11import org.greenstone.gatherer.util.Utility;
12import org.greenstone.gatherer.gui.SimpleMenuBar;
13import org.greenstone.gatherer.gui.ModalDialog;
14import org.w3c.dom.*;
15
16/** A lock file dialog is shown whenever GLI detects an existing lock on a collection it is trying to open. The user can look at the details provided with the dialog, and decide whether to 'steal' the lock or to cancel the loading. */
17public class LockFileDialog
18 extends ModalDialog {
19
20 private int choice;
21 private Document document;
22 private JButton cancel_button;
23 private JButton ok_button;
24 private LockFileDialog self;
25
26 static final public int NO_OPTION = 0;
27 static final public int YES_OPTION = 1;
28
29 static final private Dimension LABEL_SIZE = new Dimension(100, 25);
30 static final private Dimension SIZE = new Dimension(500, 250);
31 static final private int ICON_SIZE = 30;
32
33 public LockFileDialog(JFrame parent, String name, File lock_file) {
34 super(parent, get("Title"), true);
35 setSize(SIZE);
36 setJMenuBar(new SimpleMenuBar("2.3"));
37 this.self = this;
38 // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception.
39 document = Utility.parse(lock_file, false);
40 // Creation
41 JPanel content_pane = (JPanel) getContentPane();
42 JPanel upper_pane = new JPanel();
43 ImageIcon icon = Utility.getImage("lcolicn.gif");
44 ImageIcon scaled_icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_DEFAULT));
45 JLabel icon_label = new JLabel(scaled_icon);
46 JPanel title_pane = new JPanel();
47 JLabel title_one_label = new JLabel(get("General.Warning"));
48 JLabel title_two_label = new JLabel(get("Lockfile_Message_One"));
49 JPanel central_pane = new JPanel();
50 JPanel name_pane = new JPanel();
51 JLabel name_label = new JLabel(get("Name"));
52 name_label.setPreferredSize(LABEL_SIZE);
53 TextFieldLabel name_field = new TextFieldLabel(name);
54 JPanel person_pane = new JPanel();
55 JLabel person_label = new JLabel(get("User"));
56 person_label.setPreferredSize(LABEL_SIZE);
57 TextFieldLabel person_field = new TextFieldLabel(getValue("User"));
58 JPanel machine_pane = new JPanel();
59 JLabel machine_label = new JLabel(get("Machine"));
60 machine_label.setPreferredSize(LABEL_SIZE);
61 TextFieldLabel machine_field = new TextFieldLabel(getValue("Machine"));
62 JPanel created_pane = new JPanel();
63 JLabel created_label = new JLabel(get("Date"));
64 created_label.setPreferredSize(LABEL_SIZE);
65 TextFieldLabel created_field = new TextFieldLabel(getValue("Date"));
66 JLabel central_label = new JLabel(get("Lockfile_Message_Two"));
67 JPanel button_pane = new JPanel();
68 ok_button = new JButton(get("General.OK"));
69 cancel_button = new JButton(get("General.Cancel"));
70 // Connection
71 ok_button.addActionListener(new MyActionListener());
72 cancel_button.addActionListener(new MyActionListener());
73 // Layout
74 icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
75
76 title_pane.setLayout(new GridLayout(2,1,0,5));
77 title_pane.add(title_one_label);
78 title_pane.add(title_two_label);
79
80 upper_pane.setLayout(new BorderLayout());
81 upper_pane.add(icon_label, BorderLayout.WEST);
82 upper_pane.add(title_pane, BorderLayout.CENTER);
83
84 name_pane.setLayout(new BorderLayout());
85 name_pane.add(name_label, BorderLayout.WEST);
86 name_pane.add(name_field, BorderLayout.CENTER);
87
88 person_pane.setLayout(new BorderLayout());
89 person_pane.add(person_label, BorderLayout.WEST);
90 person_pane.add(person_field, BorderLayout.CENTER);
91
92 machine_pane.setLayout(new BorderLayout());
93 machine_pane.add(machine_label, BorderLayout.WEST);
94 machine_pane.add(machine_field, BorderLayout.CENTER);
95
96 created_pane.setLayout(new BorderLayout());
97 created_pane.add(created_label, BorderLayout.WEST);
98 created_pane.add(created_field, BorderLayout.CENTER);
99
100 central_pane.setLayout(new GridLayout(5,1,0,5));
101 central_pane.add(name_pane);
102 central_pane.add(person_pane);
103 central_pane.add(machine_pane);
104 central_pane.add(created_pane);
105 central_pane.add(central_label);
106
107 button_pane.setLayout(new GridLayout(1,2,5,0));
108 button_pane.add(ok_button);
109 button_pane.add(cancel_button);
110
111 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
112 content_pane.setLayout(new BorderLayout());
113 content_pane.add(upper_pane, BorderLayout.NORTH);
114 content_pane.add(central_pane, BorderLayout.CENTER);
115 content_pane.add(button_pane, BorderLayout.SOUTH);
116 // Display
117 if(parent != null) {
118 Rectangle frame_bounds = parent.getBounds();
119 setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
120 }
121 else {
122 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
123 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
124 }
125 }
126
127 public int getChoice() {
128 setVisible(true);
129 return choice;
130 }
131
132 private String getValue(String name) {
133 String value = "";
134 if(document != null) {
135 try {
136 Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name);
137 value = MSMUtils.getValue(value_element);
138 }
139 catch (Exception error) {
140 }
141 }
142 else {
143 value = get("Error");
144 }
145 return value;
146 }
147
148 private class MyActionListener
149 implements ActionListener {
150 public void actionPerformed(ActionEvent event) {
151 if(event.getSource() == ok_button) {
152 choice = YES_OPTION;
153 }
154 else {
155 choice = NO_OPTION;
156 }
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}
Note: See TracBrowser for help on using the repository browser.