add search dialog and pane

This commit is contained in:
Eyck Jentzsch 2020-10-29 07:48:35 +01:00
parent 38a046aaef
commit 6be1f018b1
9 changed files with 412 additions and 105 deletions

View File

@ -140,6 +140,7 @@ public class WaveformView implements IWaveformView {
public void mouseDown(MouseEvent e) {
if (e.button == 1) {
Entry<Integer, TrackEntry> entry = trackVerticalOffset.floorEntry(e.y);
if(entry!=null)
entry.getValue().selected=true;
} else if (e.button == 3) {
Menu topMenu= top.getMenu();
@ -191,7 +192,7 @@ public class WaveformView implements IWaveformView {
down=false;
if(start==null) return;
if((e.stateMask&SWT.MODIFIER_MASK&~SWT.SHIFT)!=0) return; //don't react on modifier except shift
if(!start.equals(end)){
if(e.button == 1 && Math.abs(e.x-start.x)>3){
asyncUpdate(e.widget);
long startTime = waveformCanvas.getTimeForOffset(start.x);
long endTime = waveformCanvas.getTimeForOffset(end.x);
@ -287,9 +288,9 @@ public class WaveformView implements IWaveformView {
case SWT.MouseDown:
start=new Point(e.x, e.y);
end=new Point(e.x, e.y);
down=true;
if((e.stateMask&SWT.MODIFIER_MASK)!=0) return; //don't react on modifier
if (e.button == 1) {
down=true;
initialSelected = waveformCanvas.getElementsAt(start);
} else if (e.button == 3) {
Menu topMenu= top.getMenu();
@ -680,15 +681,13 @@ public class WaveformView implements IWaveformView {
*/
@Override
public ISelection getSelection() {
if (currentTxSelection != null) {
ArrayList<Object> sel = new ArrayList<>();
if (currentTxSelection != null) {
sel.add(currentTxSelection);
sel.addAll(currentWaveformSelection.stream().map(e -> e.waveform).collect(Collectors.toList()));
}
// sel.addAll(currentWaveformSelection.stream().map(e -> e.waveform).collect(Collectors.toList()));
sel.addAll(currentWaveformSelection);
return new StructuredSelection(sel.toArray());
} else if (currentWaveformSelection.size()>0) {
return new StructuredSelection(currentWaveformSelection.toArray());
} else
return new StructuredSelection();
}
/* (non-Javadoc)

View File

@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2015 MINRES Technologies GmbH and others.
* Copyright (c) 2011 Google, Inc.
* 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:
* MINRES Technologies GmbH - initial API and implementation
* Google, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.wb.swt;

View File

@ -136,6 +136,7 @@
<handlers xmi:id="__99WoJebEeW09eyIbHsdvg" elementId="com.minres.scviewer.e4.application.handler.loadStoreSettings" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.LoadStoreSettingsHandler" command="_7-AIMJebEeW09eyIbHsdvg"/>
<handlers xmi:id="_x4pSEGtTEeqmlpoaaMHoiw" elementId="com.minres.scviewer.e4.application.handler.0" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.EnableHover" command="_uyeyYGtTEeqmlpoaaMHoiw"/>
<handlers xmi:id="_h3jU8BkWEeudD5MqrWoETQ" elementId="com.minres.scviewer.e4.application.handler.reloadCommand" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.ReloadHandler" command="_srACsBkREeudD5MqrWoETQ"/>
<handlers xmi:id="_gn_boBlEEeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.handler.txSearch" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.SearchHandler" command="_XDxTYBlEEeuiP60JNw0iiA"/>
<bindingTables xmi:id="_95PfvnNmEeWBq8z1Dv39LA" bindingContext="_95PfuXNmEeWBq8z1Dv39LA">
<bindings xmi:id="_95Pfv3NmEeWBq8z1Dv39LA" elementId="com.minres.scviewer.e4.application.keybinding.quit" keySequence="M1+Q" command="_95PfvHNmEeWBq8z1Dv39LA">
<tags>type:user</tags>
@ -200,6 +201,10 @@
<children xsi:type="menu:HandledMenuItem" xmi:id="_Vj4jUHXIEeWwZ-9vrAR2UQ" elementId="com.minres.scviewer.e4.application.handledmenuitem.remove" label="Remove" iconURI="platform:/plugin/com.minres.scviewer.e4.application/icons/cross.png" tooltip="Remove stream/waveform from list" command="_WUZ2wHXHEeWwZ-9vrAR2UQ">
<visibleWhen xsi:type="ui:CoreExpression" xmi:id="_f6MH0HsCEeWVM_sKoXvptg" coreExpressionId="com.minres.scviewer.e4.application.oneWaveSeleted"/>
</children>
<children xsi:type="menu:MenuSeparator" xmi:id="_KSB5EBmuEeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.menuseparator.0"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_HOLGgBmuEeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.handledmenuitem.search" label="Search Tx..." iconURI="platform:/plugin/com.minres.scviewer.e4.application/icons/find.png" command="_XDxTYBlEEeuiP60JNw0iiA">
<visibleWhen xsi:type="ui:ImperativeExpression" xmi:id="_HOLGgRmuEeuiP60JNw0iiA" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.SearchHandler"/>
</children>
</menus>
<menus xsi:type="menu:PopupMenu" xmi:id="_CxJvAHXGEeWwZ-9vrAR2UQ" elementId="com.minres.scviewer.e4.application.popupmenu.wavecontext" label="Wave Menu">
<children xsi:type="menu:HandledMenuItem" xmi:id="_7HrlwHXREeWwZ-9vrAR2UQ" elementId="com.minres.scviewer.e4.application.handledmenuitem.moveup" label="Move up" iconURI="platform:/plugin/com.minres.scviewer.e4.application/icons/up_blue.png" command="_N_sOkHXHEeWwZ-9vrAR2UQ">
@ -226,6 +231,9 @@
<visibleWhen xsi:type="ui:ImperativeExpression" xmi:id="_psvR0M1gEei6rfTGo88R-w" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.elements.WaveformPopupMenuContribution"/>
<children xsi:type="menu:DynamicMenuContribution" xmi:id="_IQZZQM3hEei6rfTGo88R-w" elementId="com.minres.scviewer.e4.application.dynamicmenucontribution.0" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.elements.WaveformPopupMenuContribution"/>
</children>
<children xsi:type="menu:HandledMenuItem" xmi:id="_NnJA4BlEEeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.handledmenuitem.search" label="Search Tx..." iconURI="platform:/plugin/com.minres.scviewer.e4.application/icons/find.png" command="_XDxTYBlEEeuiP60JNw0iiA">
<visibleWhen xsi:type="ui:ImperativeExpression" xmi:id="_Se1voBlEEeuiP60JNw0iiA" contributionURI="bundleclass://com.minres.scviewer.e4.application/com.minres.scviewer.e4.application.handlers.SearchHandler"/>
</children>
</menus>
</descriptors>
<commands xmi:id="_95PfvHNmEeWBq8z1Dv39LA" elementId="org.eclipse.ui.file.exit" commandName="Quit Command"/>
@ -268,6 +276,7 @@
</commands>
<commands xmi:id="_uyeyYGtTEeqmlpoaaMHoiw" elementId="com.minres.scviewer.e4.application.command.enablehover" commandName="Enable hover" description="Enable hover window in waveform"/>
<commands xmi:id="_srACsBkREeudD5MqrWoETQ" elementId="com.minres.scviewer.e4.application.reload" commandName="Reload Command"/>
<commands xmi:id="_XDxTYBlEEeuiP60JNw0iiA" elementId="com.minres.scviewer.e4.application.txSearch" commandName="Search Command"/>
<addons xmi:id="_95PfsnNmEeWBq8z1Dv39LA" elementId="org.eclipse.e4.core.commands.service" contributionURI="bundleclass://org.eclipse.e4.core.commands/org.eclipse.e4.core.commands.CommandServiceAddon"/>
<addons xmi:id="_95Pfs3NmEeWBq8z1Dv39LA" elementId="org.eclipse.e4.ui.contexts.service" contributionURI="bundleclass://org.eclipse.e4.ui.services/org.eclipse.e4.ui.services.ContextServiceAddon"/>
<addons xmi:id="_95PftHNmEeWBq8z1Dv39LA" elementId="org.eclipse.e4.ui.bindings.service" contributionURI="bundleclass://org.eclipse.e4.ui.bindings/org.eclipse.e4.ui.bindings.BindingServiceAddon"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 B

View File

@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2015 MINRES Technologies GmbH 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:
* MINRES Technologies GmbH - initial API and implementation
*******************************************************************************/
package com.minres.scviewer.e4.application.handlers;
import org.eclipse.e4.core.di.annotations.Evaluate;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import com.minres.scviewer.database.ui.TrackEntry;
import com.minres.scviewer.e4.application.parts.SearchTxDialog;
import com.minres.scviewer.e4.application.parts.WaveformViewer;
public class SearchHandler {
@Evaluate
public boolean evaluate(MPart activePart) {
Object obj = activePart.getObject();
if(obj instanceof WaveformViewer){
WaveformViewer wfv = (WaveformViewer)obj;
ISelection sel = wfv.getSelection();
if(sel instanceof StructuredSelection) {
Object[] elem = ((StructuredSelection)sel).toArray();
if(elem.length ==0) return false;
TrackEntry e = findTrackEntry(elem);
return e!=null && e.isStream();
}
}
return false;
}
@Execute
public void execute(Shell shell, MPart activePart){
Object obj = activePart.getObject();
if(obj instanceof WaveformViewer){
WaveformViewer wfv = (WaveformViewer)obj;
ISelection sel = wfv.getSelection();
if(sel instanceof StructuredSelection) {
TrackEntry e = findTrackEntry(((StructuredSelection)sel).toArray());
SearchTxDialog dlg = new SearchTxDialog(shell, e.getStream());
if (dlg.open() != Window.OK) return;
wfv.search(dlg.getPropName(), dlg.getPropType(), dlg.getPropValue());
}
}
}
private TrackEntry findTrackEntry(Object[] elems) {
for(Object o: elems)
if(o instanceof TrackEntry)
return (TrackEntry)o;
return null;
}
}

View File

@ -0,0 +1,185 @@
package com.minres.scviewer.e4.application.parts;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;
import com.minres.scviewer.database.DataType;
import com.minres.scviewer.database.ITxEvent;
import com.minres.scviewer.database.ITxStream;
public class SearchTxDialog extends TitleAreaDialog {
private ComboViewer propNameComboViewer = null;
private Text propValueText = null;
private String propName="";
private DataType propType=null;
private String propValue="";
private ITxStream<? extends ITxEvent> stream;
private ConcurrentHashMap<String, DataType> propNames=new ConcurrentHashMap<String, DataType>();
/**
* Create the dialog.
* @param parentShell
* @param iTxStream
*/
public SearchTxDialog(Shell parentShell, ITxStream<? extends ITxEvent> iTxStream) {
super(parentShell);
setShellStyle(SWT.BORDER | SWT.RESIZE | SWT.TITLE | SWT.APPLICATION_MODAL);
stream=iTxStream;
new Thread() {
public void run() {
stream.getEvents().values().parallelStream().forEach(evtLst -> {
evtLst.forEach(evt -> {
evt.getTransaction().getAttributes().stream().forEach(attr -> {
propNames.put(attr.getName(), attr.getDataType());
});
});
});
parentShell.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
if (propNameComboViewer!=null) {
propNameComboViewer.setInput(getEntries());
propNameComboViewer.setSelection(new StructuredSelection(propNameComboViewer.getElementAt(0)));
}
}
});
}
}.run();
}
/**
* Create contents of the dialog.
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
setMessage("Specify property name and value to search for");
setTitleImage(ResourceManager.getPluginImage("com.minres.scviewer.e4.application", "icons/Minres_logo.png"));
setTitle("Search Tx in stream");
final Composite area = (Composite) super.createDialogArea(parent);
final GridLayout gridLayout = (GridLayout) area.getLayout();
gridLayout.marginTop = 10;
gridLayout.marginBottom = 10;
final Composite container = new Composite(area, SWT.NONE);
final GridLayout gl_container = new GridLayout(2, false);
gl_container.horizontalSpacing = 2;
container.setLayout(gl_container);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
final Label header = new Label(container, SWT.CENTER | SWT.WRAP);
GridData gd_header = new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1);
gd_header.verticalIndent = 10;
header.setLayoutData(gd_header);
header.setText("Stream: "+stream.getName());
final Label propNameLabel = new Label(container, SWT.NONE);
propNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
propNameLabel.setText("Property Name:");
propNameComboViewer = new ComboViewer(container, SWT.NONE);
propNameComboViewer.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
propNameComboViewer.setContentProvider(ArrayContentProvider.getInstance());
propNameComboViewer.setLabelProvider(new LabelProvider() {
@SuppressWarnings("unchecked")
@Override
public String getText(Object element) {
Map.Entry<String, DataType> e = (Map.Entry<String, DataType>)element;
return e.getKey()+" ("+e.getValue().name()+")";
}
});
propNameComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@SuppressWarnings("unchecked")
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection sel = event.getStructuredSelection();
Map.Entry<String, DataType> e = (Map.Entry<String, DataType>)sel.getFirstElement();
propName=e.getKey();
propType=e.getValue();
}
});
propNameComboViewer.setInput(getEntries());
propNameComboViewer.setSelection(new StructuredSelection(propNameComboViewer.getElementAt(0)));
final Label propValueLabel = new Label(container, SWT.NONE);
propValueLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
propValueLabel.setText("Property Value:");
propValueText = new Text(container, SWT.BORDER);
propValueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
return area;
}
private List<Entry<String,DataType>> getEntries() {
return propNames.entrySet().stream().sorted((e1,e2)->e1.getKey().compareTo(e2.getKey())).collect(Collectors.toList());
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
final Button okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
okButton.setImage(ResourceManager.getPluginImage("com.minres.scviewer.e4.application", "icons/tick.png"));
final Button cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
cancelButton.setImage(ResourceManager.getPluginImage("com.minres.scviewer.e4.application", "icons/cross.png"));
}
protected void constrainShellSize() {
super.constrainShellSize();
getShell().setMinimumSize(getShell().computeSize(-1, -1));
}
@Override
protected void okPressed() {
//propName=propNameCombo.getItem(propNameCombo.getSelectionIndex());
propValue=propValueText.getText();
super.okPressed();
}
public String getPropName() {
return propName;
}
public DataType getPropType() {
return propType;
}
public String getPropValue() {
return propValue;
}
}

View File

@ -63,6 +63,7 @@ import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
@ -113,6 +114,7 @@ public class TransactionDetails {
/** The waveform viewer part. */
private WaveformViewer waveformViewerPart;
private Composite top;
/**
* Creates the composite.
@ -123,9 +125,10 @@ public class TransactionDetails {
public void createComposite(final Composite parent, @Optional WaveformViewer waveformViewerPart) {
this.waveformViewerPart=waveformViewerPart;
parent.setLayout(new GridLayout(1, false));
top = new Composite(parent, SWT.NONE);
top.setLayout(new GridLayout(1, false));
nameFilter = new Text(parent, SWT.BORDER);
nameFilter = new Text(top, SWT.BORDER);
nameFilter.setMessage(Messages.TransactionDetails_0);
nameFilter.addModifyListener(new ModifyListener() {
@Override
@ -141,7 +144,7 @@ public class TransactionDetails {
attributeFilter = new TxAttributeFilter();
viewSorter = new TxAttributeViewerSorter();
treeViewer = new TreeViewer(parent);
treeViewer = new TreeViewer(top);
treeViewer.setContentProvider(new TransactionTreeContentProvider());
treeViewer.setLabelProvider(new TxPropertiesLabelProvider());
treeViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
@ -225,10 +228,10 @@ public class TransactionDetails {
}
});
parent.addControlListener(new ControlAdapter() {
top.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Tree table = treeViewer.getTree();
Rectangle area = parent.getClientArea();
Rectangle area = top.getClientArea();
Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = area.width - 2*table.getBorderWidth();
if (preferredSize.y > area.height + table.getHeaderHeight()) {
@ -259,6 +262,9 @@ public class TransactionDetails {
});
}
public Control getControl() {
return top;
}
/**
* Sets the focus.
*/

View File

@ -61,7 +61,10 @@ import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
@ -287,14 +290,28 @@ public class WaveformViewer implements IFileChangeListener, IPreferenceChangeLis
sashFormTop.setWeights(new int[] {25, 75});
Composite rightTop = new Composite(sashFormRight, SWT.NONE);
Composite rightBottom = new Composite(sashFormRight, SWT.NONE);
sashFormRight.setWeights(new int[] {80, 20});
waveformPane = factory.createPanel(rightTop);
ctx.set(Composite.class, rightBottom);
detailsView = ContextInjectionFactory.make(TransactionDetails.class, ctx);
CTabFolder tabFolder = new CTabFolder(sashFormRight, SWT.BORDER);
tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
CTabItem tbtmDetails = new CTabItem(tabFolder, SWT.NONE);
tbtmDetails.setText("Transaction Details");
ctx.set(Composite.class, tabFolder);
detailsView = ContextInjectionFactory.make(TransactionDetails.class, ctx);
tbtmDetails.setControl(detailsView.getControl());
CTabItem tbtmSearchResults = new CTabItem(tabFolder, SWT.NONE);
tbtmSearchResults.setText("Search Results");
TableViewer tableViewer = new TableViewer(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
Table table = tableViewer.getTable();
tbtmSearchResults.setControl(table);
sashFormRight.setWeights(new int[] {75, 25});
tabFolder.setSelection(0);
waveformPane.setMaxTime(0);
setupColors();
@ -1294,4 +1311,9 @@ public class WaveformViewer implements IFileChangeListener, IPreferenceChangeLis
eventBroker.post(WaveStatusBarControl.MARKER_DIFF, null);
}
}
public void search(String propName, DataType type, String propValue) {
// TODO Auto-generated method stub
}
}

View File

@ -1,12 +1,13 @@
/*******************************************************************************
* Copyright (c) 2015 MINRES Technologies GmbH and others.
* Copyright (c) 2011 Google, 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:
* MINRES Technologies GmbH - initial API and implementation
* Google, Inc. - initial API and implementation
* Wim Jongman - 1.8 and higher compliance
*******************************************************************************/
package org.eclipse.wb.swt;
@ -24,6 +25,8 @@ import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.CompositeImageDescriptor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.osgi.framework.Bundle;
@ -31,44 +34,52 @@ import org.osgi.framework.Bundle;
import com.minres.scviewer.e4.application.Messages;
/**
* Utility class for managing OS resources associated with SWT/JFace controls such as colors, fonts, images,
* etc.
* Utility class for managing OS resources associated with SWT/JFace controls
* such as colors, fonts, images, etc.
*
* !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> method to release the
* operating system resources managed by cached objects when those objects and OS resources are no longer
* needed (e.g. on application shutdown)
* This class is created automatically when you fiddle around with images and
* colors in WB. You might want to prevent your application from using this
* class and provide your own more effective means of resource caching.
*
* Even though this class can be used to manage these resources, if they are
* here for the duration of the application and not used then you still have an
* effective resource leak.
*
* Application code must explicitly invoke the <code>dispose()</code> method to
* release the operating system resources managed by cached objects when those
* objects and OS resources are no longer needed.
*
* This class may be freely distributed as part of any application or plugin.
* <p>
*
* @author scheglov_ke
* @author Dan Rubel
* @author Wim Jongman
*/
public class ResourceManager extends SWTResourceManager {
////////////////////////////////////////////////////////////////////////////
//
// Image
//
////////////////////////////////////////////////////////////////////////////
private static Map<ImageDescriptor, Image> m_descriptorImageMap = new HashMap<ImageDescriptor, Image>();
/**
* Returns an {@link ImageDescriptor} stored in the file at the specified path relative to the specified
* class.
* The map where we store our images.
*/
private static Map<ImageDescriptor, Image> m_descriptorImageMap = new HashMap<ImageDescriptor, Image>();
/**
* Returns an {@link ImageDescriptor} stored in the file at the specified path
* relative to the specified class.
*
* @param clazz
* the {@link Class} relative to which to find the image descriptor.
* @param path
* the path to the image file.
* @param clazz the {@link Class} relative to which to find the image
* descriptor.
* @param path the path to the image file.
* @return the {@link ImageDescriptor} stored in the file at the specified path.
*/
public static ImageDescriptor getImageDescriptor(Class<?> clazz, String path) {
return ImageDescriptor.createFromFile(clazz, path);
}
/**
* Returns an {@link ImageDescriptor} stored in the file at the specified path.
*
* @param path
* the path to the image file.
* @param path the path to the image file.
* @return the {@link ImageDescriptor} stored in the file at the specified path.
*/
public static ImageDescriptor getImageDescriptor(String path) {
@ -78,11 +89,11 @@ public class ResourceManager extends SWTResourceManager {
return null;
}
}
/**
* Returns an {@link Image} based on the specified {@link ImageDescriptor}.
*
* @param descriptor
* the {@link ImageDescriptor} for the {@link Image}.
* @param descriptor the {@link ImageDescriptor} for the {@link Image}.
* @return the {@link Image} based on the specified {@link ImageDescriptor}.
*/
public static Image getImage(ImageDescriptor descriptor) {
@ -96,23 +107,24 @@ public class ResourceManager extends SWTResourceManager {
}
return image;
}
/**
* Maps images to decorated images.
*/
@SuppressWarnings("unchecked")
private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY];
/**
* Returns an {@link Image} composed of a base image decorated by another image.
*
* @param baseImage
* the base {@link Image} that should be decorated.
* @param decorator
* the {@link Image} to decorate the base image.
* @param baseImage the base {@link Image} that should be decorated.
* @param decorator the {@link Image} to decorate the base image.
* @return {@link Image} The resulting decorated image.
*/
public static Image decorateImage(Image baseImage, Image decorator) {
return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
}
/**
* Returns an {@link Image} composed of a base image decorated by another image.
*
@ -149,13 +161,13 @@ public class ResourceManager extends SWTResourceManager {
protected void drawCompositeImage(int width, int height) {
drawImage(createCachedImageDataProvider(baseImage), 0, 0);
if (corner == TOP_LEFT) {
drawImage(createCachedImageDataProvider(decorator), 0, 0);
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()) , 0, 0);
} else if (corner == TOP_RIGHT) {
drawImage(createCachedImageDataProvider(decorator), bib.width - dib.width, 0);
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), bib.width - dib.width, 0);
} else if (corner == BOTTOM_LEFT) {
drawImage(createCachedImageDataProvider(decorator), 0, bib.height - dib.height);
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), 0, bib.height - dib.height);
} else if (corner == BOTTOM_RIGHT) {
drawImage(createCachedImageDataProvider(decorator), bib.width - dib.width, bib.height - dib.height);
drawImage(getUnzoomedImageDataProvider(decorator.getImageData()), bib.width - dib.width, bib.height - dib.height);
}
}
@Override
@ -169,6 +181,12 @@ public class ResourceManager extends SWTResourceManager {
}
return result;
}
private static ImageDataProvider getUnzoomedImageDataProvider(ImageData imageData) {
return zoom -> zoom == 100 ? imageData : null;
}
/**
* Dispose all of the cached images.
*/
@ -202,6 +220,7 @@ public class ResourceManager extends SWTResourceManager {
m_URLImageMap.clear();
}
}
////////////////////////////////////////////////////////////////////////////
//
// Plugin images support
@ -211,23 +230,25 @@ public class ResourceManager extends SWTResourceManager {
* Maps URL to images.
*/
private static Map<String, Image> m_URLImageMap = new HashMap<String, Image>();
/**
* Provider for plugin resources, used by WindowBuilder at design time.
*/
public interface PluginResourceProvider {
URL getEntry(String symbolicName, String path);
}
/**
* Instance of {@link PluginResourceProvider}, used by WindowBuilder at design time.
* Instance of {@link PluginResourceProvider}, used by WindowBuilder at design
* time.
*/
private static PluginResourceProvider m_designTimePluginResourceProvider = null;
/**
* Returns an {@link Image} based on a plugin and file path.
*
* @param plugin
* the plugin {@link Object} containing the image
* @param name
* the path to the image within the plugin
* @param plugin the plugin {@link Object} containing the image
* @param name the path to the image within the plugin
* @return the {@link Image} stored in the file at the specified path
*
* @deprecated Use {@link #getPluginImage(String, String)} instead.
@ -244,13 +265,12 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
/**
* Returns an {@link Image} based on a {@link Bundle} and resource entry path.
*
* @param symbolicName
* the symbolic name of the {@link Bundle}.
* @param path
* the path of the resource entry.
* @param symbolicName the symbolic name of the {@link Bundle}.
* @param path the path of the resource entry.
* @return the {@link Image} stored in the file at the specified path.
*/
public static Image getPluginImage(String symbolicName, String path) {
@ -264,6 +284,7 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
/**
* Returns an {@link Image} based on given {@link URL}.
*/
@ -290,13 +311,12 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
/**
* Returns an {@link ImageDescriptor} based on a plugin and file path.
*
* @param plugin
* the plugin {@link Object} containing the image.
* @param name
* the path to th eimage within the plugin.
* @param plugin the plugin {@link Object} containing the image.
* @param name the path to th eimage within the plugin.
* @return the {@link ImageDescriptor} stored in the file at the specified path.
*
* @deprecated Use {@link #getPluginImageDescriptor(String, String)} instead.
@ -315,14 +335,15 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
/**
* Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource entry path.
* Returns an {@link ImageDescriptor} based on a {@link Bundle} and resource
* entry path.
*
* @param symbolicName
* the symbolic name of the {@link Bundle}.
* @param path
* the path of the resource entry.
* @return the {@link ImageDescriptor} based on a {@link Bundle} and resource entry path.
* @param symbolicName the symbolic name of the {@link Bundle}.
* @param path the path of the resource entry.
* @return the {@link ImageDescriptor} based on a {@link Bundle} and resource
* entry path.
*/
public static ImageDescriptor getPluginImageDescriptor(String symbolicName, String path) {
try {
@ -335,6 +356,7 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
/**
* Returns an {@link URL} based on a {@link Bundle} and resource entry path.
*/
@ -353,13 +375,12 @@ public class ResourceManager extends SWTResourceManager {
// no such resource
return null;
}
/**
* Returns an {@link URL} based on a plugin and file path.
*
* @param plugin
* the plugin {@link Object} containing the file path.
* @param name
* the file path.
* @param plugin the plugin {@link Object} containing the file path.
* @param name the file path.
* @return the {@link URL} representing the file at the specified path.
* @throws Exception
*/
@ -400,14 +421,16 @@ public class ResourceManager extends SWTResourceManager {
}
return null;
}
////////////////////////////////////////////////////////////////////////////
//
// General
//
////////////////////////////////////////////////////////////////////////////
/**
* Dispose of cached objects and their underlying OS resources. This should only be called when the cached
* objects are no longer needed (e.g. on application shutdown).
* Dispose of cached objects and their underlying OS resources. This should only
* be called when the cached objects are no longer needed (e.g. on application
* shutdown).
*/
public static void dispose() {
disposeColors();