summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/ItemFinder.java
blob: 1b0a895784ad6d43e727aff476da15ab3c1c17a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.c2kernel.gui;

import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.Lookup;
import com.c2kernel.lookup.Path;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;

public class ItemFinder extends Box implements Runnable {
    JTextField inputField;
    JButton findButton;
    JButton findNextButton;
    GridBagLayout gridbag = new GridBagLayout();
    Lookup lookup = Gateway.getLookup();
    DomainKeyConsumer defaultConsumer = null;
	DomainKeyConsumer currentConsumer = null;
    Iterator<?> matches;
    Path rootNode = MainFrame.userNode.getPath();

	static protected ImageIcon 			mFindIcon 			= null;
	static protected ImageIcon 			mNextIcon 			= null;
	static {
			try
			{
				mNextIcon =ImageLoader.findImage("next.png");
				mFindIcon =ImageLoader.findImage("find.png");
			}
			catch (Exception e)
			{
				Logger.error("Couldn't load images: " + e);
			}
		}

    public ItemFinder() {
        super(BoxLayout.X_AXIS);
        initPanel();
    }

    public void pushNewKey(String key) {
        inputField.setText(key);
        runSearch();
    }

    public void setDefaultConsumer(DomainKeyConsumer newConsumer) {
		defaultConsumer = newConsumer;
		currentConsumer = newConsumer;
    }

	public void setConsumer(DomainKeyConsumer newConsumer, String label) {
		currentConsumer = newConsumer;
        findButton.setText(label);
	}

	public void clearConsumer(DomainKeyConsumer oldConsumer) {
        if (currentConsumer == oldConsumer) {
            currentConsumer = defaultConsumer;
            findButton.setText("");
        }
	}

    private void initPanel() {

        JLabel search = new JLabel(" "+Language.translate("Search")+":");
        add(search);
        add(Box.createHorizontalStrut(7));

        inputField = new JTextField(20);
        add(inputField);
        add(Box.createHorizontalStrut(5));
        inputField.addActionListener( new ActionListener() {
             @Override
			public void actionPerformed(ActionEvent e) {
                pushNewKey(inputField.getText());
             }
        });

		findButton = new JButton(mFindIcon);//(Language.translate("Find"));
        findButton.setMargin(new Insets(2, 5, 2, 5));
        findButton.addActionListener( new ActionListener() {
             @Override
			public void actionPerformed(ActionEvent e) {
                 pushNewKey(inputField.getText());
             }
        });
        add(findButton);
        add(Box.createHorizontalStrut(5));

		findNextButton = new JButton(mNextIcon);//(Language.translate("Next"));
        findNextButton.setMargin(new Insets(2, 5, 2, 5));
        findNextButton.addActionListener( new ActionListener() {
             @Override
			public void actionPerformed(ActionEvent e) {
                nextMatch();
             }
        });
        findNextButton.setEnabled(false);
        add(findNextButton);
        add(Box.createHorizontalStrut(15));

        // create plugins
        Logger.msg(6, "ItemFinder() - creating plugins");
        String requiredListeners = Gateway.getProperties().getString("DomainKeyListeners");
        if (requiredListeners != null) {
            StringTokenizer tok = new StringTokenizer(requiredListeners, ",");
            while (tok.hasMoreTokens()) {
                String listenerName = tok.nextToken();
                Logger.msg(6, "ItemFinder() - creating a " + listenerName);
                try {
                    Class<?> listenerClass = Class.forName(listenerName);
                    DomainKeyListener newListener = (DomainKeyListener)listenerClass.newInstance();
                    newListener.init(); newListener.setConsumer(this);
                    JToggleButton listenerButton = new JToggleButton(newListener.getIcon(), false);
                    listenerButton.addItemListener(new ListenerButtonListener(newListener, listenerButton));
                    listenerButton.setMargin(new Insets(0, 2, 0, 2));
                    listenerButton.setToolTipText("Enable "+newListener.getDescription());
                    add(listenerButton);
                    add(Box.createHorizontalStrut(7));
                } catch (Exception e) {
                    Logger.error("ItemFinder() - could not create a "+listenerName+": "+e);
                }
            }
            add(Box.createHorizontalGlue());
		}
    }

    private void runSearch() {
        Thread searcher = new Thread(this);
        searcher.start();
    }

    @Override
	public void run() {
        Thread.currentThread().setName("Entity Search");
        String searchTerm = inputField.getText();
        if (searchTerm.length() == 0) return; // don't allow null searches

        findButton.setEnabled(false); findNextButton.setEnabled(false);
        MainFrame.progress.startBouncing("Searching. Please Wait");
        findNextButton.setEnabled(false);
        String term = inputField.getText();
// The following block does property searching when the field contains a colon, but that returns EntityPaths, which the tree can't handle
//        int colonPos = term.indexOf(':');
//        if (colonPos > 0)
//        	matches = lookup.search(rootNode,term.substring(0, colonPos), term.substring(colonPos+1));
//        else
        	matches = lookup.search(rootNode,term);
        if (!matches.hasNext()) {
            MainFrame.progress.stopBouncing("No results");
            currentConsumer.push(searchTerm); // for subscribers who don't care if it exists
            findButton.setEnabled(true);
            return;
        }
        MainFrame.progress.stopBouncing("Selecting first match.");
        nextMatch();

    }

    void nextMatch() {
        findButton.setEnabled(false); findNextButton.setEnabled(false);
        DomainPath nextMatch = (DomainPath)matches.next();
        try
        {
            currentConsumer.push(nextMatch);
        }
        catch (NullPointerException e)
        {
            //case the item searched is not found !
        }
        findButton.setEnabled(true);
        findNextButton.setToolTipText("Click to show next match");
        if (matches.hasNext()) findNextButton.setEnabled(true);
    }

    private class ListenerButtonListener implements ItemListener {
        private final DomainKeyListener listener;
        private final JToggleButton listenerButton;

        public ListenerButtonListener(DomainKeyListener newListener, JToggleButton listenerButton) {
            this.listener = newListener;
            this.listenerButton = listenerButton;
        }

        @Override
		public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                // Switch on
                try {
                    if (!(listener.enable())) listenerButton.doClick(); // allow plugins to disable themselves
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error initialising "+listener.getDescription(), JOptionPane.ERROR_MESSAGE);
                    listenerButton.doClick();
                }
                listenerButton.setToolTipText("Disable "+listener.getDescription());
            } else {
                // Switch off
                listener.disable();
                listenerButton.setToolTipText("Enable "+listener.getDescription());
            }
        }
    }

}