Skip to content

Commit

Permalink
Merge pull request #1598 from danilovesky/property-editor-action
Browse files Browse the repository at this point in the history
Improve handling of property editor actions
  • Loading branch information
danilovesky committed May 1, 2024
2 parents 18ea851 + 1c56cc0 commit 13fe736
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

# Platform-specific execution environment, e.g. tools and docs
# (usually symlinks to ./dist/template/*/ and ./dist/doc/)
/components
/help
/libraries
/overview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static PropertyDescriptor getComponentProperty(VisualCircuit circuit, Vi

Action rightAction = new Action(PropertyHelper.SEARCH_SYMBOL,
() -> circuit.select(component),
"Select component '" + name + "'");
"<html>Select component <i>" + name + "</i></html>");

Color backgroundColor = getComponentPropertyBackgroundColor(component);

Expand All @@ -74,7 +74,9 @@ private static PropertyDescriptor getComponentProperty(VisualCircuit circuit, Vi
component.sendNotification(new PropertyChangedEvent(component, Model.PROPERTY_NAME));
}
},
() -> new TextAction(name).setRightAction(rightAction).setBackground(backgroundColor)
() -> new TextAction(name)
.setRightAction(rightAction, false)
.setBackground(backgroundColor)
).setSpan();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.workcraft.types.Pair;
import org.workcraft.utils.BackendUtils;
import org.workcraft.utils.DialogUtils;
import org.workcraft.utils.FileUtils;
import org.workcraft.workspace.FileFilters;

import java.awt.*;
Expand Down Expand Up @@ -290,8 +289,8 @@ public class CircuitSettings extends AbstractModelSettings {

properties.add(new PropertyDeclaration<>(File.class,
PropertyHelper.BULLET_PREFIX + GATE_LIBRARY_TITLE,
value -> setGateLibrary(getBaseRelativePath(value)),
() -> getBaseRelativeFile(getGateLibrary())));
value -> setGateLibrary(BackendUtils.getBaseRelativePath(value)),
() -> BackendUtils.getBaseRelativeFile(getGateLibrary())));

properties.add(new PropertyDeclaration<>(String.class,
PropertyHelper.BULLET_PREFIX + "WAIT name, dirty input and clean handshake",
Expand Down Expand Up @@ -345,8 +344,8 @@ public class CircuitSettings extends AbstractModelSettings {

properties.add(new PropertyDeclaration<>(File.class,
PropertyHelper.BULLET_PREFIX + "Substitution rules for export",
value -> setExportSubstitutionLibrary(getBaseRelativePath(value)),
() -> getBaseRelativeFile(getExportSubstitutionLibrary())));
value -> setExportSubstitutionLibrary(BackendUtils.getBaseRelativePath(value)),
() -> BackendUtils.getBaseRelativeFile(getExportSubstitutionLibrary())));

properties.add(new PropertyDeclaration<>(Boolean.class,
PropertyHelper.BULLET_PREFIX + "Invert substitution rules for export",
Expand All @@ -355,8 +354,8 @@ public class CircuitSettings extends AbstractModelSettings {

properties.add(new PropertyDeclaration<>(File.class,
PropertyHelper.BULLET_PREFIX + "Substitution rules for import",
value -> setImportSubstitutionLibrary(getBaseRelativePath(value)),
() -> getBaseRelativeFile(getImportSubstitutionLibrary())));
value -> setImportSubstitutionLibrary(BackendUtils.getBaseRelativePath(value)),
() -> BackendUtils.getBaseRelativeFile(getImportSubstitutionLibrary())));

properties.add(new PropertyDeclaration<>(Boolean.class,
PropertyHelper.BULLET_PREFIX + "Invert substitution rules for import",
Expand Down Expand Up @@ -514,14 +513,6 @@ public Map<String, String> getChoice() {
CircuitSettings::getForkBufferPattern));
}

private static String getBaseRelativePath(File file) {
return file == null ? "" : FileUtils.stripBase(file.getPath(), System.getProperty("user.dir"));
}

private static File getBaseRelativeFile(String path) {
return (path == null) || path.isEmpty() ? null : new File(path);
}

private static void errorDescriptionFormat(String prefix, String suffix) {
DialogUtils.showError(prefix + " description format is incorrect. It should be as follows:\n" + suffix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,7 @@ private FunctionComponent createModuleInstance(Circuit circuit, VerilogInstance
component.setIsEnvironment(true);
} else if (moduleToFileNameMap != null) {
String path = moduleToFileNameMap.get(verilogModule);
FileReference refinement = new FileReference();
refinement.setPath(path);
component.setRefinement(refinement);
component.setRefinement(new FileReference(path));
}
component.setModule(verilogInstance.moduleName);
circuit.add(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ public class RefinementDependencyGraph {
private final Map<File, ModelEntry> fileToModelMap = new HashMap<>();

public RefinementDependencyGraph(WorkspaceEntry we) {
Stack<File> stack = new Stack<>();
File topFile = Framework.getInstance().getWorkspace().getFile(we);
fileToModelMap.put(topFile, we.getModelEntry());
Map<String, File> topDependencyMap = extractInstanceDependencyMap(we.getModelEntry());
stack.addAll(topDependencyMap.values());
detailedDependencyGraph.put(topFile, topDependencyMap);
this(Framework.getInstance().getWorkspace().getFile(we));
}

public RefinementDependencyGraph(File topFile) {
Stack<File> stack = new Stack<>();
try {
ModelEntry topMe = WorkUtils.loadModel(topFile);
fileToModelMap.put(topFile, topMe);
Map<String, File> topDependencyMap = extractInstanceDependencyMap(topMe);
stack.addAll(topDependencyMap.values());
detailedDependencyGraph.put(topFile, topDependencyMap);
} catch (DeserialisationException e) {
String filePath = FileUtils.getFullPath(topFile);
LogUtils.logError("Cannot read top-level file '" + filePath + "':\n" + e.getMessage());
}
Set<File> visited = new HashSet<>();
while (!stack.empty()) {
File file = stack.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static PropertyDescriptor getSymbolProperty(VisualFsm fsm, Symbol symbol
() -> {
fsm.selectNone();
fsm.addToSelection(fsm.getVisualEvents(symbol));
}, "Select all events for symbol '" + symbolName + "'");
}, "<html>Select all events for symbol <i>" + symbolName + "</i></html>");

return new PropertyDeclaration<>(TextAction.class, "Symbol " + symbolName,
value -> {
Expand All @@ -55,8 +55,7 @@ private static PropertyDescriptor getSymbolProperty(VisualFsm fsm, Symbol symbol
event.sendNotification(new PropertyChangedEvent(event, Event.PROPERTY_SYMBOL));
}
},
() -> new TextAction(symbolName)
.setRightAction(rightAction)
() -> new TextAction(symbolName).setRightAction(rightAction, false)
).setSpan();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ private static PropertyDescriptor getSignalProperty(VisualFst fst, Signal signal

Action leftAction = new Action(PropertyHelper.BULLET_SYMBOL,
() -> signal.setType(type.toggle()),
"Toggle type of signal '" + signalName + "'");
"<html>Toggle type of signal <i>" + signalName + "</i></html>");

Action rightAction = new Action(PropertyHelper.SEARCH_SYMBOL,
() -> {
fst.selectNone();
fst.addToSelection(fst.getVisualEvents(signal));
}, "Select all events of signal '" + signalName + "'");
}, "<html>Select all events of signal <i>" + signalName + "</i></html>");

return new PropertyDeclaration<>(TextAction.class, "Signal " + signalName,
value -> {
Expand All @@ -81,8 +81,10 @@ private static PropertyDescriptor getSignalProperty(VisualFst fst, Signal signal
event.sendNotification(new PropertyChangedEvent(event, Event.PROPERTY_SYMBOL));
}
},
() -> new TextAction(signalName).setLeftAction(leftAction)
.setRightAction(rightAction).setForeground(type.getColor())
() -> new TextAction(signalName)
.setLeftAction(leftAction, true)
.setRightAction(rightAction, false)
.setForeground(type.getColor())
).setSpan();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ private static PropertyDescriptor getSignalProperty(VisualStg visualStg, String

Action leftAction = new Action(PropertyHelper.BULLET_SYMBOL,
() -> mathStg.setSignalType(signal, signalType.toggle(), container),
"Toggle type of signal '" + signal + "'");
"<html>Toggle type of signal <i>" + signal + "</i></html>");

Action rightAction = new Action(PropertyHelper.SEARCH_SYMBOL,
() -> {
visualStg.selectNone();
for (SignalTransition transition : mathStg.getSignalTransitions(signal, container)) {
visualStg.addToSelection(visualStg.getVisualComponent(transition, VisualSignalTransition.class));
}
}, "Select all events of signal '" + signal + "'");
}, "<html>Select all events of signal <i>" + signal + "</i></html>");

return new PropertyDeclaration<>(TextAction.class, null,
value -> {
Expand All @@ -100,7 +100,10 @@ private static PropertyDescriptor getSignalProperty(VisualStg visualStg, String
}
}
},
() -> new TextAction(signal).setLeftAction(leftAction).setRightAction(rightAction).setForeground(color)
() -> new TextAction(signal)
.setLeftAction(leftAction, true)
.setRightAction(rightAction, false)
.setForeground(color)
).setSpan();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ private static List<String> sort(Collection<String> refs) {
private static void writeMarking(PrintWriter out, PetriModel petri, Collection<? extends Place> places,
boolean needInstanceNumbers) {

StringBuilder capacity = new StringBuilder();
for (Place p : places) {
if (p.getCapacity() != 1) {
String placeRef = getReference(petri, p, needInstanceNumbers);
capacity.append(' ').append(placeRef).append('=').append(p.getCapacity());
}
}
if (!capacity.isEmpty()) {
out.write(KEYWORD_CAPACITY + capacity + '\n');
}

ArrayList<String> markingEntries = new ArrayList<>();
for (Place place : places) {
final String reference;
Expand Down Expand Up @@ -249,16 +260,6 @@ private static void writeMarking(PrintWriter out, PetriModel petri, Collection<?
out.write(m);
}
out.write("}\n");
StringBuilder capacity = new StringBuilder();
for (Place p : places) {
if (p.getCapacity() != 1) {
String placeRef = getReference(petri, p, needInstanceNumbers);
capacity.append(' ').append(placeRef).append('=').append(p.getCapacity());
}
}
if (capacity.length() > 0) {
out.write(KEYWORD_CAPACITY + capacity + '\n');
}
}

private static void writePetri(PrintWriter out, PetriModel petri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ public class FileReference {
private String base = null;
private String path = null;

public FileReference() {
}

public FileReference(File file) {
this(file == null ? null : file.getPath());
}

public FileReference(String path) {
setPath(path);
}

public String getBase() {
return base;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ private void chooseAction() {

if (DialogUtils.showFileOpener(fc)) {
file = fc.getSelectedFile();
fileReference = new FileReference();
String path = FileUtils.getFullPath(file);
fileReference.setPath(path);
fileReference = new FileReference(FileUtils.getFullPath(file));
update();
fireEditingStopped();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class TextAction {

private final String text;
private Action leftAction;
private boolean modelModifyingLeftAction;
private Action rightAction;
private boolean modelModifyingRightAction;
private Color foreground;
private Color background;

Expand All @@ -21,23 +23,40 @@ public String getText() {
}

public TextAction setLeftAction(Action value) {
return setLeftAction(value, true);
}
public TextAction setLeftAction(Action value, boolean modifyModel) {
leftAction = value;
modelModifyingLeftAction = modifyModel;
return this;
}

public Action getLeftAction() {
return leftAction;
}

public boolean isModelModifyingLeftAction() {
return modelModifyingLeftAction;
}

public TextAction setRightAction(Action value) {
return setRightAction(value, true);
}

public TextAction setRightAction(Action value, boolean modifyModel) {
rightAction = value;
modelModifyingRightAction = modifyModel;
return this;
}

public Action getRightAction() {
return rightAction;
}

public boolean isModelModifyingRightAction() {
return modelModifyingRightAction;
}

public TextAction setForeground(Color value) {
foreground = value;
return this;
Expand All @@ -56,4 +75,5 @@ public Color getBackground() {
return background;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
leftButton.setText(leftAction.getTitle());
leftButton.setToolTipText(ActionUtils.getActionTooltip(leftAction));
leftButton.addActionListener(e -> {
// Call fireEditingStopped before the action so memento can be saved
fireEditingStopped();
// Call fireEditingStopped() before model modifying action to save memento
if (textAction.isModelModifyingLeftAction()) {
fireEditingStopped();
}
leftAction.run();
});
}
Expand All @@ -83,8 +85,10 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
rightButton.setText(rightAction.getTitle());
rightButton.setToolTipText(ActionUtils.getActionTooltip(rightAction));
rightButton.addActionListener(e -> {
// Call fireEditingStopped before the action so memento can be saved
fireEditingStopped();
// Call fireEditingStopped() before model modifying action to save memento
if (textAction.isModelModifyingRightAction()) {
fireEditingStopped();
}
rightAction.run();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public String getClassName() {
public FileReference deserialise(Element element) {
FileReference result = null;
if (element.hasAttribute("path")) {
result = new FileReference();
result.setPath(element.getAttribute("path"));
result = new FileReference(element.getAttribute("path"));
}
return result;
}
Expand Down
13 changes: 13 additions & 0 deletions workcraft/WorkcraftCore/src/org/workcraft/utils/BackendUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ public class BackendUtils {

private static final String TOOLS_DIR_NAME = "tools";
private static final String LIBRARIES_DIR_NAME = "libraries";
private static final String COMPONENTS_DIR_NAME = "components";
private static final String EXE_EXTENSION = ".exe";

public static String getBaseRelativePath(File file) {
return file == null ? "" : FileUtils.stripBase(file.getPath(), System.getProperty("user.dir"));
}

public static File getBaseRelativeFile(String path) {
return (path == null) || path.isEmpty() ? null : new File(path);
}

public static String getLibraryPath(String fileName) {
return LIBRARIES_DIR_NAME + File.separator + fileName;
}
Expand Down Expand Up @@ -48,4 +57,8 @@ public static String getTemplateToolPath(String dirName, String fileName) {
return getToolPath(dirName, fileName);
}

public static String getComponentPath(String dirName, String fileName) {
return COMPONENTS_DIR_NAME + File.separator + dirName + File.separator + fileName;
}

}

0 comments on commit 13fe736

Please sign in to comment.