package org.greenstone.gatherer.gui; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.gui.TextFieldLabel; import org.greenstone.gatherer.msm.MSMUtils; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.gui.SimpleMenuBar; import org.greenstone.gatherer.gui.ModalDialog; import org.w3c.dom.*; /** 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. */ public class LockFileDialog extends ModalDialog { private int choice; private Document document; private JButton cancel_button; private JButton ok_button; private LockFileDialog self; static final public int NO_OPTION = 0; static final public int YES_OPTION = 1; static final private Dimension LABEL_SIZE = new Dimension(100, 25); static final private Dimension SIZE = new Dimension(500, 250); static final private int ICON_SIZE = 30; public LockFileDialog(JFrame parent, String name, File lock_file) { super(parent, get("LockFileDialog.Title"), true); setSize(SIZE); setJMenuBar(new SimpleMenuBar("2.3")); this.self = this; // Parse the lock file, but do so quietly so that if the XML is poorly formed it doesn't show exception. document = Utility.parse(lock_file, false); // Creation JPanel content_pane = (JPanel) getContentPane(); JPanel upper_pane = new JPanel(); ImageIcon icon = Utility.getImage("lcolicn.gif"); ImageIcon scaled_icon = new ImageIcon(icon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_DEFAULT)); JLabel icon_label = new JLabel(scaled_icon); JPanel title_pane = new JPanel(); JLabel title_one_label = new JLabel(get("General.Warning")); JLabel title_two_label = new JLabel(get("LockFileDialog.Lockfile_Message_One")); JPanel central_pane = new JPanel(); JPanel name_pane = new JPanel(); JLabel name_label = new JLabel(get("LockFileDialog.Name")); name_label.setPreferredSize(LABEL_SIZE); TextFieldLabel name_field = new TextFieldLabel(name); name_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); JPanel person_pane = new JPanel(); JLabel person_label = new JLabel(get("LockFileDialog.User")); person_label.setPreferredSize(LABEL_SIZE); TextFieldLabel person_field = new TextFieldLabel(getValue("User")); person_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); JPanel machine_pane = new JPanel(); JLabel machine_label = new JLabel(get("LockFileDialog.Machine")); machine_label.setPreferredSize(LABEL_SIZE); TextFieldLabel machine_field = new TextFieldLabel(getValue("Machine")); machine_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); JPanel created_pane = new JPanel(); JLabel created_label = new JLabel(get("LockFileDialog.Date")); created_label.setPreferredSize(LABEL_SIZE); TextFieldLabel created_field = new TextFieldLabel(getValue("Date")); created_field.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false)); JLabel central_label = new JLabel(get("LockFileDialog.Lockfile_Message_Two")); JPanel button_pane = new JPanel(); ok_button = new JButton(get("General.OK")); cancel_button = new JButton(get("General.Cancel")); // Connection ok_button.addActionListener(new MyActionListener()); cancel_button.addActionListener(new MyActionListener()); // Layout icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,10)); title_pane.setLayout(new GridLayout(2,1,0,5)); title_pane.add(title_one_label); title_pane.add(title_two_label); upper_pane.setLayout(new BorderLayout()); upper_pane.add(icon_label, BorderLayout.WEST); upper_pane.add(title_pane, BorderLayout.CENTER); name_pane.setLayout(new BorderLayout()); name_pane.add(name_label, BorderLayout.WEST); name_pane.add(name_field, BorderLayout.CENTER); person_pane.setLayout(new BorderLayout()); person_pane.add(person_label, BorderLayout.WEST); person_pane.add(person_field, BorderLayout.CENTER); machine_pane.setLayout(new BorderLayout()); machine_pane.add(machine_label, BorderLayout.WEST); machine_pane.add(machine_field, BorderLayout.CENTER); created_pane.setLayout(new BorderLayout()); created_pane.add(created_label, BorderLayout.WEST); created_pane.add(created_field, BorderLayout.CENTER); central_pane.setLayout(new GridLayout(5,1,0,5)); central_pane.add(name_pane); central_pane.add(person_pane); central_pane.add(machine_pane); central_pane.add(created_pane); central_pane.add(central_label); button_pane.setLayout(new GridLayout(1,2,5,0)); button_pane.add(ok_button); button_pane.add(cancel_button); content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); content_pane.setLayout(new BorderLayout()); content_pane.add(upper_pane, BorderLayout.NORTH); content_pane.add(central_pane, BorderLayout.CENTER); content_pane.add(button_pane, BorderLayout.SOUTH); // Display if(parent != null) { Rectangle frame_bounds = parent.getBounds(); setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2); } else { Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2); } } public int getChoice() { setVisible(true); return choice; } private String getValue(String name) { String value = ""; if(document != null) { try { Element value_element = (Element) MSMUtils.getNodeFromNamed(document.getDocumentElement(), name); value = MSMUtils.getValue(value_element); } catch (Exception error) { } } else { value = get("LockFileDialog.Error"); } return value; } private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(event.getSource() == ok_button) { choice = YES_OPTION; } else { choice = NO_OPTION; } self.dispose(); } } static public void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(640,480); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); LockFileDialog dialog = new LockFileDialog(frame, "Test", new File(args[0])); int choice = dialog.getChoice(); } static private String get(String key) { if(key.indexOf(".") == -1) { key = "LockFileDialog." + key; } return Gatherer.dictionary.get(key); } }