Skip to content

Commit

Permalink
[Profiles Manage Dialog] Don't sort table when a new Profile is added…
Browse files Browse the repository at this point in the history
…/renamed

- Before now a new entry would jump position when renamed
- Add a sort header to the column
  • Loading branch information
Phillipus committed Jul 6, 2023
1 parent d964041 commit 6a985dd
Showing 1 changed file with 63 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -101,8 +102,7 @@ public class ProfilesManagerDialog extends ExtendedTitleAreaDialog {

private IArchimateModel fArchimateModel;

private Map<String, IProfile> fProfilesModel; // The model's Profiles
private Map<String, IProfile> fProfilesTemp; // A temporary copy of the model's Profiles
private List<IProfile> fProfilesCopy; // A temporary copy of the model's Profiles

private Map<String, List<IProfiles>> fProfilesUsage; // Usages of a Profile

Expand All @@ -126,18 +126,15 @@ public ProfilesManagerDialog(Shell parentShell, IArchimateModel model) {

fArchimateModel = model;

// Create indexed copies of the model's Profiles
fProfilesModel = new LinkedHashMap<>();
fProfilesTemp = new LinkedHashMap<>();

// Create a copy of the model's Profiles
fProfilesCopy = new ArrayList<>();
for(IProfile profile : model.getProfiles()) {
// Index of original profiles in the model
fProfilesModel.put(profile.getId(), profile);

// Copies of the original profiles
fProfilesTemp.put(profile.getId(), EcoreUtil.copy(profile));
fProfilesCopy.add(EcoreUtil.copy(profile));
}

// Sort them
sortProfiles();

// Find Profile usages now as it can be time consuming to do it more than once
fProfilesUsage = new HashMap<>();
for(Entry<IProfile, List<IProfiles>> entry : ArchimateModelUtils.findProfilesUsage(model).entrySet()) {
Expand Down Expand Up @@ -258,15 +255,6 @@ private void createTableControl(Composite parent) {
fTableViewer.getTable().setHeaderVisible(true);
fTableViewer.getTable().setLinesVisible(true);

fTableViewer.setComparator(new ViewerComparator(Collator.getInstance()) {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
return getComparator().compare(((IProfile)e1).getName(), ((IProfile)e2).getName());
}
});

// Columns

// Icon
TableViewerColumn columnIcon = new TableViewerColumn(fTableViewer, SWT.NONE, 0);
tableLayout.setColumnData(columnIcon.getColumn(), new ColumnWeightData(5, true));
Expand All @@ -276,6 +264,15 @@ public int compare(Viewer viewer, Object e1, Object e2) {
columnName.getColumn().setText(Messages.ProfilesManagerDialog_7);
tableLayout.setColumnData(columnName.getColumn(), new ColumnWeightData(50, true));
columnName.setEditingSupport(new NameEditingSupport(fTableViewer));

// Sort on name column
fTableViewer.getTable().setSortColumn(columnName.getColumn());
fTableViewer.getTable().setSortDirection(SWT.UP);

columnName.getColumn().addListener(SWT.Selection, event -> {
sortProfiles();
fTableViewer.refresh();
});

// Restricted to Concept Type
TableViewerColumn columnConceptType = new TableViewerColumn(fTableViewer, SWT.NONE, 2);
Expand All @@ -300,7 +297,7 @@ public void dispose() {

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

Expand Down Expand Up @@ -467,7 +464,7 @@ private void createNewProfile() {
IProfile profile = IArchimateFactory.eINSTANCE.createProfile();
profile.setConceptType(fDefaultClass.getName());
profile.setName(generateNewProfileName(profile.getConceptType()));
fProfilesTemp.put(profile.getId(), profile);
fProfilesCopy.add(profile);

//fTableViewer.applyEditorValue(); // complete any current editing
fTableViewer.refresh();
Expand Down Expand Up @@ -496,14 +493,28 @@ private void deleteSelectedProfiles() {
Messages.ProfilesManagerDialog_14,
Messages.ProfilesManagerDialog_15)) {

for(Object o : ((IStructuredSelection)fTableViewer.getSelection()).toList()) {
fProfilesTemp.remove(((IProfile)o).getId());
for(Object profile : ((IStructuredSelection)fTableViewer.getSelection()).toList()) {
fProfilesCopy.remove(profile);
}

fTableViewer.refresh();
}
}

/**
* Sort profiles on names
*/
private void sortProfiles() {
Collections.sort(fProfilesCopy, new Comparator<IProfile>() {
Collator collator = Collator.getInstance();

@Override
public int compare(IProfile p1, IProfile p2) {
return collator.compare(p1.getName(), p2.getName());
}
});
}

/**
* Update the image preview
*/
Expand Down Expand Up @@ -605,7 +616,7 @@ private void disposePreviewImage() {
* @return true if name is not blank and not already used for concept type
*/
private boolean isValidProfileNameAndType(String name, String conceptType) {
return !name.isBlank() && !ArchimateModelUtils.hasProfileByNameAndType(fProfilesTemp.values(), name, conceptType);
return !name.isBlank() && !ArchimateModelUtils.hasProfileByNameAndType(fProfilesCopy, name, conceptType);
}

/**
Expand All @@ -629,29 +640,43 @@ protected Point getDefaultDialogSize() {
protected void okPressed() {
super.okPressed();

CompoundCommand compoundCmd = new CompoundCommand(Messages.ProfilesManagerDialog_16);
// Create some handy lookups

// Model Profiles
Map<String, IProfile> modelProfilesMap = new HashMap<>();
for(IProfile profile : fArchimateModel.getProfiles()) {
modelProfilesMap.put(profile.getId(), profile);
}

// Our copy of Profiles
Map<String, IProfile> copyProfilesMap = new HashMap<>();
for(IProfile profile : fProfilesCopy) {
copyProfilesMap.put(profile.getId(), profile);
}

// Iterate thru our temp list of Profiles
for(IProfile profile : fProfilesTemp.values()) {
// Is this a copy of the original?
if(fProfilesModel.containsKey(profile.getId())) {
IProfile profileOriginal = fProfilesModel.get(profile.getId());
CompoundCommand compoundCmd = new CompoundCommand(Messages.ProfilesManagerDialog_16);

// Iterate thru our copied list of Profiles
for(IProfile profile : fProfilesCopy) {
// Is this profile in the model?
if(modelProfilesMap.containsKey(profile.getId())) {
IProfile profileOriginal = modelProfilesMap.get(profile.getId());

// The Profile has been edited
// Yes, but the Profile has been edited
if(!EcoreUtil.equals(profileOriginal, profile)) {
List<IProfiles> usages = fProfilesUsage.get(profileOriginal.getId());
compoundCmd.add(new ChangeProfileCommand(profileOriginal, profile, usages));
}
}
// A new Profile was added
// No, so this is a new Profile and has to be added
else {
compoundCmd.add(new AddListMemberCommand<IProfile>(fArchimateModel.getProfiles(), profile));
}
}

// Iterate thru model's Profiles and compare with our temp list to see if any Profiles were deleted
for(IProfile profile : fArchimateModel.getProfiles()) {
if(!fProfilesTemp.containsKey(profile.getId())) {
// Iterate thru model's Profiles and compare with our copied list to see if any Profiles were deleted
for(IProfile profile : modelProfilesMap.values()) {
if(!copyProfilesMap.containsKey(profile.getId())) {
List<IProfiles> usages = fProfilesUsage.get(profile.getId());
compoundCmd.add(new DeleteProfileCommand(profile, usages));
}
Expand Down Expand Up @@ -748,7 +773,7 @@ protected void setValue(Object element, Object value) {
// Allow the name to change case, but don't allow a duplicate Profile name for the same concept type
if(profile.getName().equalsIgnoreCase(name) || isValidProfileNameAndType(name, profile.getConceptType())) {
profile.setName((String)value);
fTableViewer.refresh(); // refresh the whole table so items are sorted
fTableViewer.update(element, null);
}
}
}
Expand Down

0 comments on commit 6a985dd

Please sign in to comment.