1   package com.ontotext.gate.vr.dialog;
2   
3   import com.ontotext.gate.vr.*;
4   import gate.creole.ontology.*;
5   
6   import java.awt.*;
7   import java.awt.event.*;
8   import javax.swing.*;
9   import java.util.*;
10  
11  /** Multiple Selection Dialog. This dialog allows the selection of
12   *  multiple items and performing
13   *  a desired operation over them.  */
14  public class MultipleSelectionDialog extends JDialog {
15  
16    /**
17     * the size of the dialog (x)
18     */
19    private final static int SIZE_X = 320;
20  
21    /**
22     * the size of the dialog (y)
23     */
24    private final static int SIZE_Y = 385;
25  
26  
27    /** reference to te ontology editor */
28    protected OntologyEditor editor;
29  
30    /** the list of items */
31    private Vector list;
32  
33    /** the text to be displayed */
34    private String text;
35  
36    /**the title of the dialog*/
37    private String title;
38  
39    protected GridBagLayout gridBagLayout1 = new GridBagLayout();
40    protected JList guiList = new JList();
41    protected JLabel guiText = new JLabel();
42    protected JPanel buttonsPanel = new JPanel();
43    protected JButton cancelBtn = new JButton();
44    public JButton okBtn = new JButton();
45    protected JButton noneBtn = new JButton();
46    protected JButton allBtn = new JButton();
47  
48    /**
49     * @param editor reference to the ontology editor
50     * @param list the list of items in the dialog
51     * @param text the text of the dialog
52     * @param title the title of the dialog
53     */
54    public MultipleSelectionDialog(OntologyEditor editor, Vector list,
55      String text, String title) {
56      if ( null == editor )
57        throw new gate.util.GateRuntimeException("the ontology editor reference is null");
58  
59      if ( null == list )
60        throw new gate.util.GateRuntimeException("the items list of this dialog is null");
61  
62      this.editor = editor;
63      this.list  = list;
64      this.text = text;
65      this.title = title;
66  
67      try {
68        jbInit();
69        this.setSize(SIZE_X,SIZE_Y);
70      }
71      catch(Exception e) {
72        e.printStackTrace();
73      }
74  
75  
76    } // MultipleSelectionDialog
77  
78    private void jbInit() throws Exception {
79      this.getContentPane().setLayout(gridBagLayout1);
80      guiList.setBorder(BorderFactory.createLoweredBevelBorder());
81      this.setResizable(false);
82      guiText.setAlignmentY((float) 0.0);
83      guiText.setHorizontalAlignment(SwingConstants.CENTER);
84      guiText.setHorizontalTextPosition(SwingConstants.CENTER);
85      cancelBtn.setText("Cancel");
86      okBtn.setText("OK");
87      noneBtn.setText("None");
88      allBtn.setText("All");
89      this.getContentPane().add(guiList,                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
90              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 238, 249));
91      this.getContentPane().add(guiText,                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
92              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 6, 0), 257, 21));
93      this.getContentPane().add(buttonsPanel,   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
94              ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 6));
95      buttonsPanel.add(allBtn, null);
96      buttonsPanel.add(noneBtn, null);
97      buttonsPanel.add(okBtn, null);
98      buttonsPanel.add(cancelBtn, null);
99  
100     /*end of automatic code */
101 
102     this.setTitle(title);
103     this.guiText.setText(text);
104     this.guiList.setListData(list);
105 
106     this.setSize(new Dimension(436, 331));
107 
108     allBtn.addActionListener(new BtnListener());
109     cancelBtn.addActionListener(new BtnListener());
110     noneBtn.addActionListener(new BtnListener());
111     okBtn.addActionListener(new BtnListener());
112 
113     this.addKeyListener(new EscListener());
114   } // jbInit();
115 
116   /**Button Listener*/
117   private class BtnListener implements ActionListener {
118     public void actionPerformed(ActionEvent e) {
119         if (allBtn == e.getSource()) {
120           MultipleSelectionDialog.this.guiList.setSelectionInterval(0,list.size()-1);
121         } // if all
122 
123         if (noneBtn == e.getSource() ) {
124           guiList.removeSelectionInterval(0,list.size());
125         } // if none
126 
127         if ( cancelBtn == e.getSource()) {
128           MultipleSelectionDialog.this.dispose();
129         } //if cancel
130     } //actionPerformed
131   } // class BtnListener
132 
133   /** lsitens for Esc */
134   private class EscListener implements KeyListener {
135     public void keyTyped(KeyEvent kev){};
136     public void keyReleased(KeyEvent kev) {};
137     public void keyPressed(KeyEvent kev) {
138       if (kev.VK_ESCAPE == kev.getKeyCode()) {
139         MultipleSelectionDialog.this.setVisible(false);
140       } // if escape
141     } // keyPressed
142   }  // class EscListener
143 
144 } // class MultipleSelectionDialog