Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4f7fdaa5ebfc2344284c1a77acaddc3e8ed9821 (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.te.tcf.ui.dialogs;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.persistence.history.HistoryManager;
import org.eclipse.tcf.te.runtime.persistence.utils.DataHelper;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IDelegateService;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepAttributes;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService;
import org.eclipse.tcf.te.tcf.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.ui.handler.images.ActionHistoryImageDescriptor;
import org.eclipse.tcf.te.tcf.ui.interfaces.IDefaultContextToolbarDelegate;
import org.eclipse.tcf.te.tcf.ui.interfaces.IPreferenceKeys;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.jface.images.AbstractImageDescriptor;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * ActionHistorySelectionDialog
 */
public class ActionHistorySelectionDialog extends AbstractArraySelectionDialog {

	private static final int EDIT_ID = IDialogConstants.CLIENT_ID;
	private static final int COPY_ID = IDialogConstants.CLIENT_ID + 1;

	MenuItem menuExecute = null;
	MenuItem menuEdit = null;
	MenuItem menuCopy = null;
	MenuItem menuRemove = null;

	private int maxEntries = -1;

	protected static class Entry {
		IPeerNode peerNode;
		IDefaultContextToolbarDelegate delegate;
		String data;
	}

	/**
	 * Constructor.
	 * @param shell
	 * @param contextHelpId
	 */
	public ActionHistorySelectionDialog(Shell shell, String contextHelpId) {
		super(shell, contextHelpId);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#createDialogAreaContent(org.eclipse.swt.widgets.Composite)
	 */
	@SuppressWarnings("unused")
    @Override
	protected void createDialogAreaContent(Composite parent) {
	    super.createDialogAreaContent(parent);

        Menu menu = new Menu(getViewer().getTable());

        menuEdit = new MenuItem(menu, SWT.PUSH);
        menuEdit.setText(Messages.ActionHistorySelectionDialog_button_copy);
        menuEdit.addSelectionListener(new SelectionAdapter() {
        	@Override
        	public void widgetSelected(SelectionEvent e) {
        	    editPressed();
        	}
		});

        menuCopy = new MenuItem(menu, SWT.PUSH);
        menuCopy.setText(Messages.ActionHistorySelectionDialog_button_edit);
        menuCopy.addSelectionListener(new SelectionAdapter() {
        	@Override
        	public void widgetSelected(SelectionEvent e) {
        	    copyPressed();
        	}
		});

        menuExecute = new MenuItem(menu, SWT.PUSH);
        menuExecute.setText(Messages.ActionHistorySelectionDialog_button_execute);
        menuExecute.addSelectionListener(new SelectionAdapter() {
        	@Override
        	public void widgetSelected(SelectionEvent e) {
        	    okPressed();
        	}
		});

        new MenuItem(menu, SWT.SEPARATOR);

        menuRemove = new MenuItem(menu, SWT.PUSH);
        menuRemove.setText(Messages.ActionHistorySelectionDialog_button_remove);
        menuRemove.addSelectionListener(new SelectionAdapter() {
        	@Override
        	public void widgetSelected(SelectionEvent e) {
        	    removePressed();
        	}
		});
        menuRemove.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE));

        getViewer().getTable().setMenu(menu);

	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, COPY_ID, Messages.ActionHistorySelectionDialog_button_copy, false);
		createButton(parent, EDIT_ID, Messages.ActionHistorySelectionDialog_button_edit, false);
		createButton(parent, IDialogConstants.OK_ID, Messages.ActionHistorySelectionDialog_button_execute, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	@Override
	protected void buttonPressed(int buttonId) {
		if (EDIT_ID == buttonId) {
			editPressed();
		}
		else if (COPY_ID == buttonId) {
			copyPressed();
		}
		else {
		    super.buttonPressed(buttonId);
		}
	}

	protected void removePressed() {
		Entry entry = getSelectedEntry();
		if (entry != null) {
			HistoryManager.getInstance().remove(IStepAttributes.PROP_LAST_RUN_HISTORY_ID + "@" + entry.peerNode.getPeerId(), entry.data); //$NON-NLS-1$
			getViewer().setInput(getInput());
			getViewer().refresh();
		}
	}

	protected void copyPressed() {
		Entry entry = getSelectedEntry();
		if (entry != null) {
			if (entry.delegate.execute(entry.peerNode, entry.data, true) != null) {
				close();
			}
		}
	}

	protected void editPressed() {
		Entry entry = getSelectedEntry();
		if (entry != null) {
			String newData = entry.delegate.execute(entry.peerNode, entry.data, true);
			if (newData != null) {
				if (!entry.data.equals(newData)) {
					HistoryManager.getInstance().remove(IStepAttributes.PROP_LAST_RUN_HISTORY_ID + "@" + entry.peerNode.getPeerId(), entry.data); //$NON-NLS-1$
				}
				close();
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		Entry entry = getSelectedEntry();
	    super.okPressed();
		if (entry != null) {
			if (entry.delegate.execute(entry.peerNode, entry.data, false) != null) {
				close();
			}
		}
	}

	protected Entry getSelectedEntry() {
		ISelection sel = viewer.getSelection();
		if (sel instanceof IStructuredSelection && !((IStructuredSelection)sel).isEmpty()) {
			Object first = ((IStructuredSelection)sel).getFirstElement();
			if (first instanceof Entry) {
				return (Entry)first;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#updateEnablement(org.eclipse.jface.viewers.TableViewer)
	 */
	@Override
	protected void updateEnablement(TableViewer viewer) {
		if (viewer.getTable().getSelectionCount() == 0) {
			viewer.getTable().setSelection(0);
		}

	    Entry entry = getSelectedEntry();

	    // Adjust the OK button enablement
	    Button okButton = getButton(IDialogConstants.OK_ID);
	    SWTControlUtil.setEnabled(okButton, entry != null && entry.delegate.validate(entry.peerNode, entry.data));
	    if (menuExecute != null) {
		    menuExecute.setEnabled(entry != null && entry.delegate.validate(entry.peerNode, entry.data));
	    }

	    // Adjust the edit button enablement
	    Button editButton = getButton(EDIT_ID);
	    SWTControlUtil.setEnabled(editButton, entry != null);
	    if (menuEdit != null) {
		    menuEdit.setEnabled(entry != null);
	    }

	    // Adjust the copy button enablement
	    Button copyButton = getButton(COPY_ID);
	    SWTControlUtil.setEnabled(copyButton, entry != null);
	    if (menuCopy != null) {
		    menuCopy.setEnabled(entry != null);
	    }

	    if (menuRemove != null) {
		    menuRemove.setEnabled(entry != null);
	    }
	}

	protected int getMaxEntries() {
		if (maxEntries <= 0) {
			maxEntries = UIPlugin.getScopedPreferences().getInt(IPreferenceKeys.PREF_MAX_RECENT_ACTION_ENTRIES);
		}
		return maxEntries;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#getInput()
	 */
	@Override
	protected Object[] getInput() {
		final IPeerNode peerNode = ServiceManager.getInstance().getService(IDefaultContextService.class).getDefaultContext(null);

		IService[] services = ServiceManager.getInstance().getServices(peerNode, IDelegateService.class, false);
		Map<String, IDefaultContextToolbarDelegate> delegates = new LinkedHashMap<String, IDefaultContextToolbarDelegate>();
		for (IService service : services) {
	        if (service instanceof IDelegateService) {
	        	IDefaultContextToolbarDelegate delegate = ((IDelegateService)service).getDelegate(peerNode, IDefaultContextToolbarDelegate.class);
	        	if (delegate != null) {
        			for (String stepGroupId : delegate.getHandledStepGroupIds(peerNode)) {
        				if (!delegates.containsKey(stepGroupId)) {
        					delegates.put(stepGroupId, delegate);
        				}
                    }
	        	}
	        }
        }

		List<Entry> actions = new ArrayList<Entry>();
    	String[] entries = HistoryManager.getInstance().getHistory(IStepAttributes.PROP_LAST_RUN_HISTORY_ID + "@" + peerNode.getPeerId()); //$NON-NLS-1$
    	if (entries != null && entries.length > 0) {
    		int count = 0;
    		for (final String entry : entries) {
    			if (count >= getMaxEntries()) {
    				break;
    			}
    			IPropertiesContainer decoded = DataHelper.decodePropertiesContainer(entry);
    			String stepGroupId = decoded.getStringProperty(IStepAttributes.ATTR_STEP_GROUP_ID);
    			if (stepGroupId != null && delegates.containsKey(stepGroupId)) {
    				Entry action = new Entry();
    				action.peerNode = peerNode;
    				action.delegate = delegates.get(stepGroupId);
    				action.data = entry;
         			actions.add(action);
         			count++;
    			}
    		}
    	}

		return actions.toArray();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#getLabelProvider()
	 */
	@Override
	protected IBaseLabelProvider getLabelProvider() {
	    return new CellLabelProvider() {
	    	/* (non-Javadoc)
	    	 * @see org.eclipse.jface.viewers.CellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
	    	 */
	    	@Override
	    	public void update(ViewerCell cell) {
	    		cell.setText(" " + getText(cell.getElement())); //$NON-NLS-1$
	    		cell.setImage(getImage(cell.getElement()));
	    	}

	    	/* (non-Javadoc)
	    	 * @see org.eclipse.jface.viewers.CellLabelProvider#getToolTipText(java.lang.Object)
	    	 */
	    	@Override
            public String getToolTipText(Object element) {
	    		Entry entry = (Entry)element;
    			return entry.delegate.getDescription(entry.peerNode, entry.data);
	    	}

	    	public String getText(Object element) {
	    		Entry entry = (Entry)element;
    			return entry.delegate.getLabel(entry.peerNode, entry.data);
	    	}

	    	public Image getImage(Object element) {
	    		Entry entry = (Entry)element;
    			AbstractImageDescriptor descriptor = new ActionHistoryImageDescriptor(
    							UIPlugin.getDefault().getImageRegistry(),
    							entry.delegate.getImage(entry.peerNode, entry.data),
    							entry.delegate.validate(entry.peerNode, entry.data));
    			return UIPlugin.getSharedImage(descriptor);
	    	}
	    };
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#getDialogTitle()
	 */
	@Override
	protected String getDialogTitle() {
		return Messages.ActionHistorySelectionDialog_dialogTitle;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#getTitle()
	 */
	@Override
	protected String getTitle() {
		IPeerNode peerNode = ServiceManager.getInstance().getService(IDefaultContextService.class).getDefaultContext(null);
		return NLS.bind(Messages.ActionHistorySelectionDialog_title, peerNode.getName());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#getDefaultMessage()
	 */
	@Override
	protected String getDefaultMessage() {
		return Messages.ActionHistorySelectionDialog_message;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#updateSelection(org.eclipse.jface.viewers.ISelection)
	 */
    @Override
    protected void updateSelection(ISelection selection) {
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.ui.dialogs.AbstractArraySelectionDialog#createButtonAreaContent(org.eclipse.swt.widgets.Composite)
	 */
    @Override
    protected void createButtonAreaContent(Composite parent) {
    }

}

Back to the top