Commit 28221d7e authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Fix #42: in "ScenarioPanel.java", group model names in GUI under menu "Insert...

Fix #42: in "ScenarioPanel.java", group model names in GUI under menu "Insert model name" (in "Model" tab).

Note: no unit tests have been written for new methods in "ClassFinder.java".
parent cb43db3d
......@@ -266,7 +266,8 @@ Tab.Model.loadTemplateMenu.title=Load template
Tab.Model.confirmLoadTemplate.title=Continue?
Tab.Model.confirmLoadTemplate.text=This replaces the content of the text field.
Tab.Model.addAttributesMenu.title=Add Attributes
Tab.Model.insertModelNameMenu.title=Insert model-name
Tab.Model.insertModelNameMenu.title=Insert model name
Tab.Model.insertModelNameSubMenu.title=Main models
Tab.Pedestrians.title=Pedestrians
Tab.Topography.title=Topography
Tab.OutputProcessors.title=Data processing
......
......@@ -266,6 +266,7 @@ Tab.Model.confirmLoadTemplate.title=Fortfahren?
Tab.Model.confirmLoadTemplate.text=Der Inhalt des Textfeldes wird ersetzt.
Tab.Model.addAttributesMenu.title=Attributes hinzuf\u00fcgen
Tab.Model.insertModelNameMenu.title=Model-Name einf\u00fcgen
Tab.Model.insertModelNameSubMenu.title=Hauptmodelle
Tab.Pedestrians.title=Fu\u00dfg\u00e4nger
Tab.Topography.title=Topographie
Tab.OutputProcessors.title=Datenverarbeitung
......
......@@ -21,6 +21,7 @@ import java.net.URL;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class ClassFinder {
......@@ -43,6 +44,12 @@ public class ClassFinder {
modelNames.removeAll(getMainModelNames());
return modelNames;
}
public static Map<String, List<String>> groupPackages(List<String> classNamesInPackageNotation) {
List<String> groupNames = deriveGroupNamesFromPackageNames(classNamesInPackageNotation);
Map<String, List<String>> groupNamesToMembers = sortClassNamesIntoGroups(classNamesInPackageNotation, groupNames);
return groupNamesToMembers;
}
// all output file classes
public static List<Class<?>> getOutputFileClasses() {
......@@ -222,4 +229,37 @@ public class ClassFinder {
return null;
}
private static List<String> deriveGroupNamesFromPackageNames(List<String> classNamesInPackageNotation) {
List<String> groupNames = new ArrayList<String>();
// Use characters until last dot as group names.
for (String classNameInPackageNotation : classNamesInPackageNotation) {
int lastDotPosition = classNameInPackageNotation.lastIndexOf(".");
if (lastDotPosition >= 0) {
String groupName = classNameInPackageNotation.substring(0, lastDotPosition);
groupNames.add(groupName);
}
}
return groupNames;
}
private static Map<String, List<String>> sortClassNamesIntoGroups(List<String> classNamesInPackageNotation, List<String> groupNames) {
TreeMap<String, List<String>> groupNamesToMembers = new TreeMap<>();
for (String groupName : groupNames) {
List<String> groupMembers = classNamesInPackageNotation.stream().filter(name -> name.startsWith(groupName)).sorted().collect(Collectors.toList());
groupNamesToMembers.put(groupName, groupMembers);
}
List<String> modelNamesWithoutPackage = classNamesInPackageNotation.stream().filter(name -> name.lastIndexOf(".") == -1).sorted().collect(Collectors.toList());
if (modelNamesWithoutPackage.size() > 0) {
groupNamesToMembers.put("...", modelNamesWithoutPackage);
}
return groupNamesToMembers;
}
}
......@@ -21,6 +21,8 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
......@@ -140,26 +142,42 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro
}
}
})));
JMenu mnModelNameMenu = new JMenu(Messages.getString("Tab.Model.insertModelNameMenu.title"));
presetMenuBar.add(mnModelNameMenu);
menusInTabs.add(mnModelNameMenu);
JMenu submenuMainModels = new JMenu(Messages.getString("Tab.Model.insertModelNameSubMenu.title"));
mnModelNameMenu.add(submenuMainModels);
ClassFinder.getMainModelNames().stream()
.sorted()
.forEach(className -> mnModelNameMenu.add(new JMenuItem(new AbstractAction(className + " (MainModel)") {
.forEach(className -> submenuMainModels.add(new JMenuItem(new AbstractAction(className) {
@Override
public void actionPerformed(ActionEvent e) {
attributesModelView.insertAtCursor("\"" + className + "\"");
}
})));
ClassFinder.getModelNames().stream()
.sorted()
.forEach(className -> mnModelNameMenu.add(new JMenuItem(new AbstractAction(className) {
}
)));
Map<String, List<String>> groupedPackages = ClassFinder.groupPackages(ClassFinder.getModelNames());
for (Map.Entry<String, List<String>> entry : groupedPackages.entrySet()) {
JMenu currentSubMenu = new JMenu(entry.getKey());
for (String className : entry.getValue()) {
currentSubMenu.add(new JMenuItem(new AbstractAction(className) {
@Override
public void actionPerformed(ActionEvent e) {
attributesModelView.insertAtCursor("\"" + className + "\"");
}
})));
attributesModelView.getPanelTop().add(presetMenuBar, 0); // the 0 puts it at the leftest position instead of the rightest
}));
}
mnModelNameMenu.add(currentSubMenu);
}
attributesModelView.getPanelTop().add(presetMenuBar, 0); // the 0 puts it at the leftmost position instead of the rightmost
tabbedPane.addTab(Messages.getString("Tab.Model.title"), attributesModelView);
topographyFileView = new TextView("/scenarios", "default_directory_scenarios", AttributeType.TOPOGRAPHY);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment