Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nbproject/private/
out/
.idea/workspace.xml
.idea/tasks.xml
/bin/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
PNEditor (Petri Net editor)
========
Status of the different branches :

- Invariant : adds a token limit on places
- MacroRecorder : abandonned branche (a version of macro that had modifications from invariant)
- MacroRecorderClean : adds a macro manager
- MacroLimit : integrates the token limit and the macro manager
- macroAndTokenLimit : integrates the token limit, the macro manager and the fire N modification
from https://github.com/e17goudi/pneditor. due to some modifications on the marking class there,
this version compile but has some bugs (try to find them !)



You can download PNEditor from [www.pneditor.org](http://www.pneditor.org/)

Expand Down
188 changes: 188 additions & 0 deletions src/org/pneditor/editor/MacroManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright (C) 2008-2010 Martin Riesz <riesz.martin at gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.pneditor.editor;

import java.util.ArrayList;
import java.util.List;
import org.pneditor.editor.actions.RecordMacroAction;
import org.pneditor.editor.actions.PlayMacroAction;
import org.pneditor.util.Command;
import org.pneditor.util.GraphicsTools;
import javax.swing.AbstractAction;

import org.pneditor.util.RecordableCommand;


/**
* MacroManager manages macro recording and playing
*
* @author Ladislas Ducerf <ladislas.ducerf at gmail.com>
*/
public class MacroManager {


//the list of commands, once recording is done
private List<RecordableCommand> recordedCommands = new ArrayList<RecordableCommand>();
// list of commands being recorded
private List<RecordableCommand> buffer = new ArrayList<RecordableCommand>();
/*
* The separation of the two list allows for the saved macro to be played during
* recording, so the new one can be composed of the old one
*/


private Root root;
private PlayMacroAction playMacroAction;
private boolean recording;

/**
* Constructs a new MacroManager
*
* @param root Root object
* @param playMacroAction action for play macro button
*/
public MacroManager(Root root, PlayMacroAction playMacroAction) {
this.root = root;
this.playMacroAction = playMacroAction;
this.recording = false;
}

/**
* Records a command in the buffer if it implements the RecordableCommand
* interface
*
* @param command the command to be recorded
*/
public void recordCommand(Command command) {
//Do we want macro to be sensitive to undo/redo during recording ?
// Currently they are not
if(isRecordableCommand(command) ) {
buffer.add((RecordableCommand) command);
//currentCommandIndex = buffer.size() - 1;
}
}

/**
* Returns true if a commands implements the RecordableCommand interface
*
* @param command the command to be tested
* @return
*/
public boolean isRecordableCommand(Command command) {
return (command instanceof RecordableCommand);
}

/**
* Puts the macroManager in recording mode and prepares everything
*/
public void beginRecording() {
this.recording = true;
eraseBuffer();
refresh();
}

/**
* Puts the macroManager out of recording mode and saves the buffer
*/
public void endRecording() {
this.recording = false;
copyBufferToRecordedCommands();
refresh();
}


/**
* Identifies if all elements of the macro (places/nodes) are still existing
*
* @return false if at least one element is missing, true otherwise
*/
public boolean macroUnaffected() {
boolean unaffected = true;
for (RecordableCommand command : recordedCommands) {
if(! root.getDocument().petriNet.getCurrentSubnet().getElements().contains(command.getRecordedElement())) {
unaffected = false;
break;
}
}
return unaffected;
}

/**
* Undo a played macro. Called by the undo method of the PlayMacro command
*/
public void undoMacro() {
for (int i = recordedCommands.size() - 1 ; i >= 0 ; i --) {
Command command = recordedCommands.get(i);
command.undo();
refresh();
}
}


/**
* Execute a macro. Called by the execute method of the PlayMacro command
*/
public void playMacro() {
for (Command command : recordedCommands) {
command.execute();
refresh();
}

}

/**
* Erases all commands from the buffer.
*/
public void eraseBuffer() {
buffer = new ArrayList<RecordableCommand>();
}

/**
* Copy the buffer to the recorded commands
*/
public void copyBufferToRecordedCommands() {
recordedCommands = new ArrayList<RecordableCommand>(buffer);
}

/**
* Return the number of commands saved
*
* @return the size of recordedCommands
*/
public int getRecordedCommandsNumber() {
return recordedCommands.size();
}


public boolean getRecording() {
return recording;
}


private void refresh() {
root.refreshAll();
}

public void refreshPlayIcon() {
if (macroUnaffected()) {
playMacroAction.putValue(AbstractAction.SMALL_ICON, GraphicsTools.getIcon("pneditor/macroPlay.gif"));
} else {
playMacroAction.putValue(AbstractAction.SMALL_ICON, GraphicsTools.getIcon("pneditor/macroPlayUncertain.gif"));
}
}

}
27 changes: 24 additions & 3 deletions src/org/pneditor/editor/Root.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public UndoManager getUndoManager() {
return undoManager;
}

// Macro manager - per tab
protected RecordMacroAction recordMacro = new RecordMacroAction(this);
protected PlayMacroAction playMacro = new PlayMacroAction(this);
private MacroManager macroManager = new MacroManager(this, playMacro);

public MacroManager getMacroManager() {
return macroManager;
}

// Current directory - per application
private File currentDirectory;

Expand Down Expand Up @@ -310,13 +319,17 @@ public void refreshAll() {
canvas.repaint();
enableOnlyPossibleActions();
getRoleEditor().refreshSelected();
getMacroManager().refreshPlayIcon();
}

public void repaintCanvas() {
canvas.repaint();
}

private void enableOnlyPossibleActions() {



boolean isDeletable = clickedElement != null
&& !(clickedElement instanceof ReferencePlace)
|| !selection.isEmpty()
Expand All @@ -335,13 +348,15 @@ private void enableOnlyPossibleActions() {
boolean roleSelected = !roleEditor.getSelectedElements().isEmpty();
boolean isParent = !document.petriNet.isCurrentSubnetRoot();
boolean isPtoT = false;

boolean macroCurrentlyRecording = getMacroManager().getRecording();
boolean macroExists = (getMacroManager().getRecordedCommandsNumber()!=0 );


if (isArc) {
Arc test;
test = (Arc) clickedElement;
isPtoT = test.isPlaceToTransition();
}

cutAction.setEnabled(isCutable);
copyAction.setEnabled(isCopyable);
pasteAction.setEnabled(isPastable);
Expand All @@ -362,6 +377,8 @@ private void enableOnlyPossibleActions() {
undo.setEnabled(getUndoManager().isUndoable());
redo.setEnabled(getUndoManager().isRedoable());
setPlaceStatic.setEnabled(isPlaceNode);

playMacro.setEnabled(macroExists&(!macroCurrentlyRecording));
}

@Override
Expand Down Expand Up @@ -521,7 +538,11 @@ private void setupMainFrame() {
toolBar.addSeparator();
toolBar.add(addSelectedTransitionsToSelectedRoles);
toolBar.add(removeSelectedTransitionsFromSelectedRoles);

toolBar.addSeparator();

toolBar.add(recordMacro);
toolBar.add(playMacro);

JMenuBar menuBar = new JMenuBar();
mainFrame.setJMenuBar(menuBar);

Expand Down
3 changes: 3 additions & 0 deletions src/org/pneditor/editor/UndoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public void executeCommand(Command command) {
executedCommands.add(command);
currentCommandIndex = executedCommands.size() - 1;
command.execute();
if (root.getMacroManager().getRecording()) {
root.getMacroManager().recordCommand(command);
}
refresh();
root.setModified(true);
}
Expand Down
50 changes: 50 additions & 0 deletions src/org/pneditor/editor/actions/PlayMacroAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2008-2010 Martin Riesz <riesz.martin at gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.pneditor.editor.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import org.pneditor.editor.Root;
import org.pneditor.editor.commands.PlayMacroCommand;
import org.pneditor.petrinet.Element;
import org.pneditor.util.GraphicsTools;

/**
*
* @author Ladislas Ducerf <ladislas.ducerf at gmail.com>
*/
public class PlayMacroAction extends AbstractAction {

private Root root;

public PlayMacroAction(Root root) {
this.root = root;
String name = "Play macro";
putValue(NAME, name);
putValue(SMALL_ICON, GraphicsTools.getIcon("pneditor/macroPlay.gif"));
putValue(SHORT_DESCRIPTION, name);
setEnabled(false);
}

public void actionPerformed(ActionEvent e) {
root.getUndoManager().executeCommand(new PlayMacroCommand(root.getMacroManager()));
}
}
Loading