1ce12f55f4530950f7fc6ddc73d1867ebb954d356showardpackage autotest.tko;
2ce12f55f4530950f7fc6ddc73d1867ebb954d356showard
30d92da0fe19a095fc5678c4159e6a1756df65e48showardimport com.google.gwt.event.dom.client.ChangeEvent;
40d92da0fe19a095fc5678c4159e6a1756df65e48showardimport com.google.gwt.event.dom.client.ChangeHandler;
5a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showardimport com.google.gwt.event.dom.client.ClickEvent;
6a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showardimport com.google.gwt.event.dom.client.ClickHandler;
779a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.CloseEvent;
879a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.CloseHandler;
979a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.OpenEvent;
1079a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.OpenHandler;
11ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.DOM;
12ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.Window;
138a7f36583afe076a7198d1b34fe109aa491dc277jamesrenimport com.google.gwt.user.client.ui.Anchor;
14ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.Button;
15ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.Composite;
16ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.DisclosurePanel;
17ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.HorizontalPanel;
18ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.Panel;
19ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport com.google.gwt.user.client.ui.TextArea;
20ce12f55f4530950f7fc6ddc73d1867ebb954d356showard
21ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport java.util.ArrayList;
22ce12f55f4530950f7fc6ddc73d1867ebb954d356showardimport java.util.Map;
23ce12f55f4530950f7fc6ddc73d1867ebb954d356showard
24e5ae165c706c465f08a732a361ea0aa5d75075eashoward// TODO(showard): combine this code with similar code from autotest.afe.CreateJobView
25ce12f55f4530950f7fc6ddc73d1867ebb954d356showardpublic class FilterStringViewer extends Composite {
268a7f36583afe076a7198d1b34fe109aa491dc277jamesren
27ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public static final String VIEW_FILTER_STRING = "View Filter String";
28ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public static final String HIDE_FILTER_STRING = "Hide Filter String";
29ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public static final String EDIT_FILTER_STRING = "Edit Filter String";
30ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public static final String UNEDIT_FILTER_STRING = "Revert Filter String";
318a7f36583afe076a7198d1b34fe109aa491dc277jamesren
32ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public static interface EditListener {
33ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        public void onEdit();
34ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        public void onRevert();
35ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
368a7f36583afe076a7198d1b34fe109aa491dc277jamesren
378a7f36583afe076a7198d1b34fe109aa491dc277jamesren    private Anchor view = new Anchor(VIEW_FILTER_STRING);
38ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private Button edit = new Button(EDIT_FILTER_STRING);
39ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private TextArea queries = new TextArea();
40ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private DisclosurePanel queriesPanel = new DisclosurePanel();
41ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private boolean filterEdited = false;
42ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private boolean viewerEditable = false;
43ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private ArrayList<EditListener> listeners = new ArrayList<EditListener>();
448a7f36583afe076a7198d1b34fe109aa491dc277jamesren
45ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public FilterStringViewer() {
46a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showard        edit.addClickHandler(new ClickHandler() {
47a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showard            public void onClick(ClickEvent event) {
48ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                changeEditable(true);
49ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
50ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        });
518a7f36583afe076a7198d1b34fe109aa491dc277jamesren
52ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queries.setSize("35em", "10em");
53ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queries.setReadOnly(true);
540d92da0fe19a095fc5678c4159e6a1756df65e48showard        queries.addChangeHandler(new ChangeHandler() {
550d92da0fe19a095fc5678c4159e6a1756df65e48showard            public void onChange(ChangeEvent event) {
56ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                filterEdited = true;
57ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
58ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        });
598a7f36583afe076a7198d1b34fe109aa491dc277jamesren
60ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        Panel viewerHeaderPanel = new HorizontalPanel();
61ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        viewerHeaderPanel.add(view);
62ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        viewerHeaderPanel.add(edit);
638a7f36583afe076a7198d1b34fe109aa491dc277jamesren
64ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queriesPanel.setHeader(viewerHeaderPanel);
65ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queriesPanel.add(queries);
668a7f36583afe076a7198d1b34fe109aa491dc277jamesren
6779a7b0d387aac103fc1d125353eefa361030452ashoward        queriesPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
6879a7b0d387aac103fc1d125353eefa361030452ashoward            public void onClose(CloseEvent<DisclosurePanel> e) {
69ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                view.setText(VIEW_FILTER_STRING);
70ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
7179a7b0d387aac103fc1d125353eefa361030452ashoward        });
7279a7b0d387aac103fc1d125353eefa361030452ashoward        queriesPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
7379a7b0d387aac103fc1d125353eefa361030452ashoward            public void onOpen(OpenEvent<DisclosurePanel> e) {
74ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                view.setText(HIDE_FILTER_STRING);
75ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
76ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        });
778a7f36583afe076a7198d1b34fe109aa491dc277jamesren
78ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        initWidget(queriesPanel);
79ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
808a7f36583afe076a7198d1b34fe109aa491dc277jamesren
81ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public void setText(String text) {
82ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queries.setText(text);
83ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
848a7f36583afe076a7198d1b34fe109aa491dc277jamesren
85ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public String getText() {
86ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        return queries.getText();
87ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
888a7f36583afe076a7198d1b34fe109aa491dc277jamesren
89ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    public void addEditListener(EditListener listener) {
90ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        listeners.add(listener);
91ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
928a7f36583afe076a7198d1b34fe109aa491dc277jamesren
93ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    protected void addToHistory(Map<String, String> args, String prefix) {
94ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        args.put(prefix + "_viewerOpen", String.valueOf(queriesPanel.isOpen()));
95ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        args.put(prefix + "_viewerEditable", String.valueOf(viewerEditable));
96ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (viewerEditable) {
97ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            args.put(prefix + "_viewerEdited", String.valueOf(filterEdited));
98ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            if (filterEdited) {
99ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                args.put(prefix + "_viewerText", queries.getText());
100ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
101ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
102ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
1038a7f36583afe076a7198d1b34fe109aa491dc277jamesren
104ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    protected void handleHistoryArguments(Map<String, String> args, String prefix) {
105ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        // No _viewerOpen parameter. This is a preconfig without a specified custom filter.
106ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (args.get(prefix + "_viewerOpen") == null) {
107ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            queriesPanel.setOpen(false);
108ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            if (viewerEditable) {
109ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                changeEditable(false);
110ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
111ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            return;
112ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
1138a7f36583afe076a7198d1b34fe109aa491dc277jamesren
114ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        queriesPanel.setOpen(Boolean.parseBoolean(args.get(prefix + "_viewerOpen")));
115ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (viewerEditable) {
116ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            changeEditable(false);
117ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
118ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (Boolean.parseBoolean(args.get(prefix + "_viewerEditable"))) {
119ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            changeEditable(false);
120ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            filterEdited = Boolean.parseBoolean(args.get(prefix + "_viewerEdited"));
121ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            if (filterEdited) {
122ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                queries.setText(args.get(prefix + "_viewerText"));
123ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
124ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
125ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
1268a7f36583afe076a7198d1b34fe109aa491dc277jamesren
127ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    // Change the viewer's editable state
128ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    private void changeEditable(boolean clicked) {
129ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (clicked) {
130d2ef40969bbfff6ed5803aec4dd39688f70c654cshoward            DOM.eventGetCurrentEvent().stopPropagation();
131ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
1328a7f36583afe076a7198d1b34fe109aa491dc277jamesren
133ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        if (viewerEditable) {
134ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            // We only want the confirmation on revert from an edited viewer, and only if "revert"
135ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            // was clicked (not on programmatic revert)
1368a7f36583afe076a7198d1b34fe109aa491dc277jamesren            boolean userCancelled = filterEdited && clicked
137e5ae165c706c465f08a732a361ea0aa5d75075eashoward                && !Window.confirm("Are you sure you want to revert your changes?");
138e5ae165c706c465f08a732a361ea0aa5d75075eashoward            if (userCancelled) {
139ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                return;
140ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
1418a7f36583afe076a7198d1b34fe109aa491dc277jamesren
142ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            viewerEditable = false;
143ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            filterEdited = false;
144ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            queries.setReadOnly(true);
145ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            edit.setText(EDIT_FILTER_STRING);
146ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            for (EditListener listener : listeners) {
147ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                listener.onRevert();
148ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
149ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        } else {
150ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            viewerEditable = true;
151ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            queries.setReadOnly(false);
152ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            edit.setText(UNEDIT_FILTER_STRING);
153ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            queriesPanel.setOpen(true);
154ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            for (EditListener listener : listeners) {
155ce12f55f4530950f7fc6ddc73d1867ebb954d356showard                listener.onEdit();
156ce12f55f4530950f7fc6ddc73d1867ebb954d356showard            }
157ce12f55f4530950f7fc6ddc73d1867ebb954d356showard        }
158ce12f55f4530950f7fc6ddc73d1867ebb954d356showard    }
159ce12f55f4530950f7fc6ddc73d1867ebb954d356showard}
160