Skip to content

Commit 5496f1d

Browse files
committed
Add set-name
1 parent 2870964 commit 5496f1d

5 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/main/ChildModel.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ public abstract class ChildModel {
2525
private Procedure reporterRunner;
2626
private Procedure commandRunner;
2727
private LoadingCache<String, Reporter> tasks;
28+
private String name;
29+
private int modelID;
2830

29-
public ChildModel(World parentWorld) throws ExtensionException {
31+
public ChildModel(World parentWorld, int modelID) throws ExtensionException {
3032
this.parentWorld = parentWorld;
33+
this.modelID = modelID;
3134

3235
tasks = CacheBuilder.newBuilder().build(new CacheLoader<String, Reporter>() {
3336
@Override
@@ -46,6 +49,7 @@ public Reporter load(String code) throws ExtensionException {
4649
* @throws ExtensionException
4750
*/
4851
void init() throws ExtensionException {
52+
setName(workspace().getModelFileName());
4953
try {
5054
reporterRunner = workspace().compileReporter("runresult task [ 0 ]");
5155
commandRunner = workspace().compileCommands("run task []");
@@ -152,8 +156,19 @@ public String getPath() {
152156
return workspace().getModelPath();
153157
}
154158

159+
String getFrameTitle() {
160+
return name + " (LevelSpace model #" + modelID + ")";
161+
}
162+
155163
public String getName() {
156-
return workspace().modelNameForDisplay();
164+
return name;
165+
}
166+
167+
public void setName(String name) {
168+
this.name = name;
169+
if (this.frame() != null) {
170+
this.frame().setTitle(getFrameTitle());
171+
}
157172
}
158173

159174
abstract public void setSpeed(double d);

src/main/GUIChildModel.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ public class GUIChildModel extends ChildModel {
1414

1515
final JFrame frame = new JFrame();
1616
InterfaceComponent component;
17-
int levelsSpaceNumber;
1817
GUIPanel panel;
1918

2019

2120
public GUIChildModel(World parentWorld, final String path, final int levelsSpaceNumber)
2221
throws InterruptedException, ExtensionException, HaltException {
23-
super(parentWorld);
24-
this.levelsSpaceNumber = levelsSpaceNumber;
22+
super(parentWorld, levelsSpaceNumber);
2523

2624
component = runUISafely(new Callable<InterfaceComponent>() {
2725
public InterfaceComponent call() throws Exception {
@@ -43,7 +41,6 @@ public InterfaceComponent call() throws Exception {
4341
((SpeedSliderPanel) co).setValue(0);
4442
}
4543
}
46-
frame.setTitle(component.workspace().getModelFileName() + " (LevelsSpace model-id: " + String.valueOf(levelsSpaceNumber) + ")");
4744
frame.pack();
4845
// Make sure that the model doesn't close if people accidentally click the close button
4946
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

src/main/HeadlessChildModel.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class HeadlessChildModel extends ChildModel {
1313

1414
HeadlessWorkspace myWS;
1515
ImageFrame frame;
16-
int levelsSpaceNumber;
1716

1817
public HeadlessChildModel(World parentWorld, String path, final int levelsSpaceNumber) throws IOException, CompilerException, LogoException, ExtensionException {
19-
super(parentWorld);
20-
this.levelsSpaceNumber = levelsSpaceNumber;
18+
super(parentWorld, levelsSpaceNumber);
2119
myWS = HeadlessWorkspace.newInstance();
2220
myWS.open(path);
2321
init();
@@ -30,8 +28,7 @@ private void ensureImageFrame() {
3028
@Override
3129
public ImageFrame call() throws Exception {
3230
final BufferedImage bi = myWS.exportView();
33-
final String aTitle = getName().concat(" (LevelsSpace Model No. ").concat(Integer.toString(levelsSpaceNumber)).concat(")");
34-
return new ImageFrame(bi, aTitle);
31+
return new ImageFrame(myWS.exportView(), getFrameTitle());
3532
}
3633
});
3734
} catch (Exception e) {

src/main/LevelsSpace.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void load(PrimitiveManager primitiveManager) throws ExtensionException {
6363
primitiveManager.addPrimitive("load-headless-model", new LoadModel<HeadlessChildModel>(HeadlessChildModel.class));
6464
primitiveManager.addPrimitive("load-gui-model", new LoadModel<GUIChildModel>(GUIChildModel.class));
6565
primitiveManager.addPrimitive("name-of", new ModelName());
66+
primitiveManager.addPrimitive("set-name", new SetName());
6667
primitiveManager.addPrimitive("close", new CloseModel());
6768
primitiveManager.addPrimitive("models", new AllModels());
6869
primitiveManager.addPrimitive("model-exists?", new ModelExists());
@@ -452,13 +453,23 @@ public static class ModelName extends DefaultReporter{
452453
public Syntax getSyntax(){
453454
return Syntax.reporterSyntax(new int[] {Syntax.NumberType()},
454455
Syntax.StringType());
455-
456456
}
457457
public Object report(Argument[] args, Context context) throws ExtensionException, LogoException {
458458
int modelNumber = args[0].getIntValue();
459459
return getModel(modelNumber).getName();
460460
}
461+
}
462+
463+
public static class SetName extends DefaultCommand {
461464

465+
@Override
466+
public Syntax getSyntax() {
467+
return Syntax.commandSyntax(new int[] {Syntax.NumberType(), Syntax.StringType()});
468+
}
469+
@Override
470+
public void perform(Argument[] args, Context context) throws LogoException, ExtensionException {
471+
getModel(args[0].getIntValue()).setName(args[1].getString());
472+
}
462473
}
463474

464475
// this returns the path of the model

tests.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ ls-*-descendant-works-for-one-item-list
5656
O> ls:load-headless-model "Blank.nlogo"
5757
O> ls:ask-descendant [0] "crt 1"
5858
"count turtles" ls:of-descendant [0] => 1
59+
60+
ls-set-name
61+
extensions [ ls ]
62+
O> ls:load-headless-model "Blank.nlogo"
63+
O> ls:set-name 0 "foo"
64+
ls:name-of 0 => "foo"

0 commit comments

Comments
 (0)