Skip to content

Commit

Permalink
Use a dialog to select User Properties for the Search Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Dec 9, 2024
1 parent fbfc2c2 commit 6bfda22
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ void removePropertiesFilter(String key) {
fPropertiesFilter.remove(key);
}

Set<String> getPropertiesFilter() {
return fPropertiesFilter;
}

void resetPropertiesFilter() {
fPropertiesFilter.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.TreeExpansionEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
Expand Down Expand Up @@ -75,7 +76,6 @@ public class SearchWidget extends Composite {

private List<IAction> fConceptActions = new ArrayList<>();

private MenuManager fPropertiesMenu;
private MenuManager fSpecializationsMenu;

private Timer fKeyDelayTimer;
Expand Down Expand Up @@ -233,9 +233,21 @@ public void run() {
dropDownAction.add(fActionFilterDocumentation);

// Properties
fPropertiesMenu = new MenuManager(Messages.SearchWidget_5);
dropDownAction.add(fPropertiesMenu);
populatePropertiesMenu();
IAction actionProperties = new Action(Messages.SearchWidget_5, IAction.AS_PUSH_BUTTON) {
@Override
public void run() {
UserPropertiesKeySelectionDialog dialog = new UserPropertiesKeySelectionDialog(getShell(), getAllUniquePropertyKeys(),
new ArrayList<>(fSearchFilter.getPropertiesFilter()));
if(dialog.open() != Window.CANCEL) {
fSearchFilter.resetPropertiesFilter();
for(String key : dialog.getSelectedKeys()) {
fSearchFilter.addPropertiesFilter(key);
}
refreshTree();
}
}
};
dropDownAction.add(actionProperties);

dropDownAction.add(new Separator());

Expand Down Expand Up @@ -315,26 +327,26 @@ public void run() {
dropDownAction.add(new Separator());

// Show All Folders
IAction action = new Action(Messages.SearchWidget_12, IAction.AS_CHECK_BOX) {
IAction actionFolders = new Action(Messages.SearchWidget_12, IAction.AS_CHECK_BOX) {
@Override
public void run() {
fSearchFilter.setShowAllFolders(isChecked());
refreshTree();
}
};
action.setChecked(fSearchFilter.isShowAllFolders());
dropDownAction.add(action);
actionFolders.setChecked(fSearchFilter.isShowAllFolders());
dropDownAction.add(actionFolders);

dropDownAction.add(new Separator());

// Reset
action = new Action(Messages.SearchWidget_13) {
IAction actionReset = new Action(Messages.SearchWidget_13) {
@Override
public void run() {
reset();
}
};
dropDownAction.add(action);
dropDownAction.add(actionReset);

// Need to update toolbar manager now
toolBarmanager.update(true);
Expand All @@ -350,9 +362,6 @@ private void reset() {
// Don't filter on Documentation menu item
fActionFilterDocumentation.setChecked(false);

// Clear & uncheck Properties menu items
populatePropertiesMenu();

// Clear & uncheck Specializations menu items
populateSpecializationsMenu();

Expand All @@ -376,7 +385,6 @@ private void reset() {
*/
public void softReset() {
// Clear & Reset Properties
populatePropertiesMenu();
fSearchFilter.resetPropertiesFilter();

// Clear & Reset Specializations
Expand Down Expand Up @@ -414,56 +422,33 @@ public ImageDescriptor getImageDescriptor() {
return action;
}

private void populatePropertiesMenu() {
fPropertiesMenu.removeAll();

// Models that are loaded are the ones in the Models Tree
Set<String> set = new LinkedHashSet<>(); // LinkedHashSet is faster when sorting

for(IArchimateModel model : IEditorModelManager.INSTANCE.getModels()) {
getAllUniquePropertyKeysForModel(model, set);
}

List<String> list = new ArrayList<>(set);

// Sort alphabetically, but don't use Collator.getInstance() as it's too slow
list.sort((s1, s2) -> s1.compareToIgnoreCase(s2));

// Limit to a sensible menu size
if(list.size() > 1000) {
list = list.subList(0, 999);
}

for(String key : list) {
IAction action = new Action(key, IAction.AS_CHECK_BOX) {
@Override
public void run() {
if(isChecked()) {
fSearchFilter.addPropertiesFilter(key);
}
else {
fSearchFilter.removePropertiesFilter(key);
}
refreshTree();
}
};

fPropertiesMenu.add(action);
}

fPropertiesMenu.update(true);
}

private void getAllUniquePropertyKeysForModel(IArchimateModel model, Set<String> set) {
for(Iterator<EObject> iter = model.eAllContents(); iter.hasNext();) {
EObject element = iter.next();
if(element instanceof IProperty property) {
String key = property.getKey();
if(StringUtils.isSetAfterTrim(key)) {
set.add(key);
}
private List<String> getAllUniquePropertyKeys() {
// Maximum amount of items to display when getting all unique keys
final int MAX_ITEMS = 10000;

Set<String> set = new LinkedHashSet<>(); // LinkedHashSet is faster when sorting

for(IArchimateModel model : IEditorModelManager.INSTANCE.getModels()) {
for(Iterator<EObject> iter = model.eAllContents(); iter.hasNext();) {
EObject element = iter.next();
if(element instanceof IProperty property) {
String key = property.getKey();
if(StringUtils.isSetAfterTrim(key)) {
set.add(key);
if(set.size() > MAX_ITEMS) { // Don't get more than this
break;
}
}
}
}
}

List<String> list = new ArrayList<>(set);

// Sort alphabetically, but don't use Collator.getInstance() as it's too slow
list.sort((s1, s2) -> s1.compareToIgnoreCase(s2));

return list;
}

private void populateSpecializationsMenu() {
Expand Down Expand Up @@ -586,7 +571,6 @@ public void dispose() {
fViewer = null;
fSearchFilter = null;
fConceptActions = null;
fPropertiesMenu = null;
fSpecializationsMenu = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package com.archimatetool.editor.views.tree.search;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.archimatetool.editor.ui.IArchiImages;
import com.archimatetool.editor.ui.components.ExtendedTitleAreaDialog;



/**
* User Properties Key Selection Dialog
*
* @author Phillip Beauvoir
*/
public class UserPropertiesKeySelectionDialog extends ExtendedTitleAreaDialog {

private static String HELP_ID = "com.archimatetool.help.userProperties"; //$NON-NLS-1$

private CheckboxTableViewer tableViewer;
private Button buttonSelectAll, buttonDeselectAll;

private List<String> keys;
private List<String> selectedKeys;

/**
* @param parentShell
* @param keys The list of property keys
* @param selected A list of property keys to select. Can be null.
*/
public UserPropertiesKeySelectionDialog(Shell parentShell, List<String> keys, List<String> selected) {
super(parentShell, "UserPropertiesSelectionDialog"); //$NON-NLS-1$
setTitleImage(IArchiImages.ImageFactory.getImage(IArchiImages.ECLIPSE_IMAGE_IMPORT_PREF_WIZARD));
setShellStyle(getShellStyle() | SWT.RESIZE);

this.keys = keys;
selectedKeys = selected;
}

@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Select Properties");
}

@Override
protected Control createButtonBar(Composite parent) {
Control c = super.createButtonBar(parent);
if(keys.size() == 0) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
buttonSelectAll.setEnabled(false);
buttonDeselectAll.setEnabled(false);
}
return c;
}

@Override
protected Control createDialogArea(Composite parent) {
// Help
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, HELP_ID);

setTitle("Select Properties");
setMessage("Select Property Keys for Filter");

Composite composite = (Composite)super.createDialogArea(parent);

Composite client = new Composite(composite, SWT.NULL);
GridLayout layout = new GridLayout(2, false);
client.setLayout(layout);
client.setLayoutData(new GridData(GridData.FILL_BOTH));

createTableControl(client);
createButtonPanel(client);

return composite;
}

private void createTableControl(Composite parent) {
Composite tableComp = new Composite(parent, SWT.BORDER);
TableColumnLayout tableLayout = new TableColumnLayout();
tableComp.setLayout(tableLayout);
tableComp.setLayoutData(new GridData(GridData.FILL_BOTH));

tableViewer = CheckboxTableViewer.newCheckList(tableComp, SWT.MULTI | SWT.FULL_SELECTION);
tableViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));

tableViewer.getTable().setLinesVisible(true);

// Columns
TableViewerColumn columnKey = new TableViewerColumn(tableViewer, SWT.NONE, 0);
tableLayout.setColumnData(columnKey.getColumn(), new ColumnWeightData(100, true));

// Content Provider
tableViewer.setContentProvider(new IStructuredContentProvider() {
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

@Override
public Object[] getElements(Object inputElement) {
return keys.toArray();
}

@Override
public void dispose() {
}
});

// Label Provider
tableViewer.setLabelProvider(new LabelProvider());

tableViewer.setInput(keys);

if(selectedKeys != null) {
tableViewer.setCheckedElements(selectedKeys.toArray());
}
}

private void createButtonPanel(Composite parent) {
Composite client = new Composite(parent, SWT.NULL);

GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
client.setLayout(layout);

GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
client.setLayoutData(gd);

buttonSelectAll = new Button(client, SWT.PUSH);
buttonSelectAll.setText("Select All");
gd = new GridData(GridData.FILL_HORIZONTAL);
buttonSelectAll.setLayoutData(gd);
buttonSelectAll.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
tableViewer.setCheckedElements(keys.toArray());
}));

buttonDeselectAll = new Button(client, SWT.PUSH);
buttonDeselectAll.setText("Deselect All");
gd = new GridData(GridData.FILL_HORIZONTAL);
buttonDeselectAll.setLayoutData(gd);
buttonDeselectAll.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
tableViewer.setCheckedElements(new Object[] {});
}));
}

@Override
protected void buttonPressed(int buttonId) {
selectedKeys = new ArrayList<>();
for(Object o : tableViewer.getCheckedElements()) {
selectedKeys.add((String)o);
}

setReturnCode(buttonId);
close();
}

public List<String> getSelectedKeys() {
return selectedKeys;
}

@Override
protected void okPressed() {
super.okPressed();

}

@Override
protected Point getDefaultDialogSize() {
return new Point(400, 250);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SearchWidget_17=Views
SearchWidget_2=Documentation
SearchWidget_3=Search in Documentation
SearchWidget_4=Filter Options
SearchWidget_5=Properties
SearchWidget_5=Properties...
SearchWidget_6=Business
SearchWidget_7=Application
SearchWidget_8=Technology && Physical
Expand Down

0 comments on commit 6bfda22

Please sign in to comment.