• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision18 (tree)
Time2017-01-28 02:43:51
Authorki-chi

Log Message

ConsolePicocalcの補完機能修正、Contextの切り出し

Change Summary

Incremental Difference

--- trunk/build.xml (revision 17)
+++ trunk/build.xml (revision 18)
@@ -18,7 +18,7 @@
1818 <property name="exeDir" value="exe"/>
1919 <property name="libDir" value="lib"/>
2020
21- <property name="jpicosheetJar" value="C:/Users/nishikawa.MEGATRADE/Apps/Eclipse-workspace-private/consolePicocalc/jarfile/JPicosheet-0.1.4.jar"/>
21+ <property name="jpicosheetJar" value="C:\Users\Yusuke\Develop\Java\PicoSheet-Workspace\JPicosheet\jarfile\JPicosheet-0.1.4.jar"/>
2222 <!-- <property name="jpicosheetJar" value="/home/yusuke/Develop/Eclipse-workspaces/PicoSheet-Workspace/JPicosheet/jarfile/JPicosheet-0.1.4.jar"/ -->
2323
2424 <target name="all" depends="init,makedir,copyJarFile,copylib,jar,makeExe,clean" />
--- trunk/exe/conpico.bat (revision 17)
+++ trunk/exe/conpico.bat (revision 18)
@@ -1,11 +1,11 @@
11 echo off
22
33 set EXEDIR=%~dp0
4-set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.9.jar
4+set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.14.2.jar
55
66 echo %CLASSPATH%
77
8-java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc
8+java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc %1
99 rem java -classpath %CLASSPATH% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc
1010
1111 pause
--- trunk/exe/conpico_debug.bat (revision 17)
+++ trunk/exe/conpico_debug.bat (revision 18)
@@ -1,11 +1,15 @@
11 echo off
22
33 set EXEDIR=%~dp0
4-set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.9.jar
4+set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.14.2.jar
55
66 echo %CLASSPATH%
77
8+set SYSPROP=-Djline.internal.Log.trace=true
9+
10+echo %SYSPROP%
11+
812 rem java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc
9-java -classpath %CLASSPATH% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc
13+java -classpath %CLASSPATH% %SYSPROP% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc %1
1014
1115 pause
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/Command.java (revision 17)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/Command.java (revision 18)
@@ -8,7 +8,7 @@
88 {".help", "doHelp", ".help\nヘルプを表示します。"},
99
1010 {".history", "doHistory", ".history\nコマンド履歴を表示します。"},
11- {".!", "doExecHistory", ".! <[履歴番号]>\n指定した履歴番号の履歴を再実行します。"},
11+/* {".!", "doExecHistory", ".! <[履歴番号]>\n指定した履歴番号の履歴を再実行します。"},*/
1212
1313 {".book.add", "doBookAdd", ".book.add <Book名>\n新しいブックを追加します。"},
1414 {".book.current", "doBookCurrent", ".book.current <Book名>\nカレントブックを変更します。"},
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/CompleterOfCellNames.java (revision 17)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/CompleterOfCellNames.java (revision 18)
@@ -16,12 +16,14 @@
1616
1717 @Override
1818 public int complete(String buffer, int cursor, List<CharSequence> candidates) {
19+ int completed = -1;
1920 for (String cellName: _sheet.getCellNames()) {
2021 if (cellName.startsWith(buffer)) {
2122 candidates.add(cellName);
23+ completed = 0;
2224 }
2325 }
24- return 0;
26+ return completed;
2527 }
2628
2729 }
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/CompleterOfCommands.java (nonexistent)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/CompleterOfCommands.java (revision 18)
@@ -0,0 +1,28 @@
1+package com.nissy_ki_chi.ConsolePicocalc;
2+
3+import java.util.List;
4+
5+import jline.console.completer.Completer;
6+
7+
8+public class CompleterOfCommands implements Completer {
9+
10+ String[] _commandNames = null;
11+
12+ public CompleterOfCommands(CommandFragment commands) {
13+ _commandNames = commands.getCommandNameList();
14+ }
15+
16+ @Override
17+ public int complete(String buffer, int cursor, List<CharSequence> candidates) {
18+ int completed = -1;
19+ for (String commandName: _commandNames) {
20+ if (commandName.startsWith(buffer)) {
21+ candidates.add(commandName);
22+ completed = 0;
23+ }
24+ }
25+ return completed;
26+ }
27+
28+}
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/ConsolePicocalc.java (revision 17)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/ConsolePicocalc.java (revision 18)
@@ -7,7 +7,6 @@
77 import java.io.IOException;
88 import java.io.InputStreamReader;
99 import java.io.UnsupportedEncodingException;
10-import java.lang.reflect.Array;
1110 import java.lang.reflect.InvocationTargetException;
1211 import java.lang.reflect.Method;
1312 import java.nio.file.Files;
@@ -14,16 +13,16 @@
1413 import java.nio.file.Paths;
1514 import java.util.ArrayList;
1615 import java.util.Arrays;
17-import java.util.Collections;
1816 import java.util.HashMap;
1917 import java.util.LinkedList;
2018 import java.util.List;
21-import java.util.Map;
22-import java.util.SortedSet;
2319 import java.util.TreeSet;
20+import java.util.regex.Pattern;
2421
2522 import com.nissy_ki_chi.jpicosheet.core.Book;
23+import com.nissy_ki_chi.jpicosheet.core.Cell;
2624 import com.nissy_ki_chi.jpicosheet.core.ConcurrentBookWrapper;
25+import com.nissy_ki_chi.jpicosheet.core.Group;
2726 import com.nissy_ki_chi.jpicosheet.core.Sheet;
2827 import com.nissy_ki_chi.jpicosheet.util.SimpleReader;
2928 import com.nissy_ki_chi.jpicosheet.util.SimpleWriter;
@@ -32,27 +31,15 @@
3231 import jline.console.completer.ArgumentCompleter;
3332 import jline.console.completer.ArgumentCompleter.ArgumentDelimiter;
3433 import jline.console.completer.ArgumentCompleter.ArgumentList;
35-import jline.console.completer.Completer;
36-import jline.console.completer.StringsCompleter;
34+import jline.console.history.History;
35+import jline.console.history.History.Entry;
3736
38-public class ConsolePicocalc {
3937
4038
41- Map<String, Book> _books = new HashMap<String, Book>();
42- String _currentBookName = null;
39+public class ConsolePicocalc {
4340
44- BookCommand _bookCommand = new BookCommand();
45- SheetCommand _sheetCommand = new SheetCommand();
46- CellCommand _cellCommand = new CellCommand();
47- GroupCommand _groupCommand = new GroupCommand();
48- TableCommand _tableCommand = new TableCommand();
49-
50- LinkedList<String> _cmdHistory = new LinkedList<>();
51- LinkedList<String> _aggregatedHistory = new LinkedList<>();
52- String _nextCommand = null;
41+ Context ctx = null;
5342
54- CommandFragment _commandStorage = new CommandFragment(".");
55-
5643 private static String DEFAULT_BOOK_NAME = "myBook";
5744 private static String DEFAULT_SHEET_NAME = "Sheet1";
5845
@@ -82,8 +69,28 @@
8269
8370
8471 private void setup() {
72+
73+ ctx = new Context();
74+ ctx.setIn(System.in);
75+ ctx.setOut(System.out);
8576
77+ ctx.set_currentBookName(DEFAULT_BOOK_NAME);
78+ ctx.set_books(new HashMap<String, Book>());
79+ ctx.get_books().put(ctx.get_currentBookName(), new Book(ctx.get_currentBookName()));
80+ ctx.set_bookCommand(new BookCommand());
81+ ctx.set_sheetCommand(new SheetCommand());
82+ ctx.set_cellCommand(new CellCommand());
83+ ctx.set_groupCommand(new GroupCommand());
84+ ctx.set_tableCommand(new TableCommand());
85+ ctx.set_nextCommand(null);
86+
87+ ctx.set_cmdHistory(new LinkedList<>());
88+ ctx.set_aggregatedHistory(new LinkedList<>());
89+
90+ CommandFragment _commandStorage = new CommandFragment(".");
8691 makeCommandStorage(_commandStorage);
92+ ctx.set_commandStorage(_commandStorage);
93+
8794 }
8895
8996 private void makeCommandStorage(CommandFragment commandStorage) {
@@ -98,19 +105,19 @@
98105
99106 public void start(String filePath) {
100107
101- Book book = null;
108+// Book book = null;
102109 // 引数としてファイルが渡された場合はそれをロードする
103110 if (filePath != null && Files.exists(Paths.get(filePath))) {
104- _currentBookName = DEFAULT_BOOK_NAME;
105- book = new Book(_currentBookName);
106- _bookCommand.load(book, filePath);
111+// _currentBookName = DEFAULT_BOOK_NAME;
112+// book = new Book(_currentBookName);
113+ ctx.get_bookCommand().load(ctx.getCurrentBook(), filePath);
107114 } else {
108115 // そうでない場合はデフォルトシートを作成
109- book = new Book(DEFAULT_BOOK_NAME);
110- book.addSheet(DEFAULT_SHEET_NAME);
111- this._currentBookName = book.getName();
116+// book = new Book(DEFAULT_BOOK_NAME);
117+ ctx.getCurrentBook().addSheet(DEFAULT_SHEET_NAME);
118+ ctx.set_currentBookName(ctx.getCurrentBook().getName());
112119 }
113- this._books.put(book.getName(), book);
120+// this._books.put(book.getName(), book);
114121
115122 // コンソールから1行づつ読み込んで処理する
116123 InputStreamReader isr = null;
@@ -117,10 +124,11 @@
117124 // BufferedReader br = null;
118125 ConsoleReader consoleReader = null;
119126 try {
120- consoleReader = new ConsoleReader();
127+// consoleReader = new ConsoleReader();
128+ ctx.setConsole(new ConsoleReader());
121129 // List<Completer> completers = new LinkedList<>();
122130
123- String[] commands = this._commandStorage.getCommandNameList();
131+ String[] commands = ctx.get_commandStorage().getCommandNameList();
124132
125133 ArgumentDelimiter ad = new ArgumentDelimiter() {
126134
@@ -131,10 +139,47 @@
131139
132140 @Override
133141 public ArgumentList delimit(CharSequence arg0, int arg1) {
134- String[] arguments = arg0.toString().split("\\s");
135- ArgumentList al = new ArgumentList( new String[] {"appl", "bppl"}, 0, 0, 2);
136-// ArgumentList al = new ArgumentList(arguments, arguments.length-1, 0, arg0.length());
137-// ArgumentList al = new ArgumentList(arguments, 0, arg0.length()-1, arg0.length()-1);
142+
143+ int cursorArgumentIndex = -1;
144+ int argumentPosition = -1;
145+ int bufferPosition = arg1;
146+ ArrayList<String> list = new ArrayList<>();
147+ StringBuilder sb = new StringBuilder();
148+ for (int i = 0; i < arg0.length(); i++) {
149+ if (!isDelimiter(arg0, i)) {
150+ sb.append(arg0.charAt(i));
151+ if (i == arg1-1) {
152+ cursorArgumentIndex = list.size()-1;
153+ }
154+ } else {
155+ list.add(sb.toString());
156+ sb.setLength(0);
157+ if (i == arg1-1) {
158+ argumentPosition = sb.length();
159+ }
160+ }
161+ }
162+ if (sb.length() > 0) {
163+ list.add(sb.toString());
164+ argumentPosition = sb.length();
165+ }
166+
167+ if (cursorArgumentIndex == -1) {
168+ cursorArgumentIndex = 0;
169+ }
170+ if (argumentPosition == -1) {
171+ argumentPosition = 0;
172+ }
173+ ArgumentList al = new ArgumentList(list.toArray(new String[]{}), cursorArgumentIndex, argumentPosition, bufferPosition);
174+
175+
176+// Pattern p = Pattern.compile("(([^\\s]+)((\\s+)|$)+)");
177+//
178+// String[] arguments = arg0.toString().split("\\s+");
179+// int posOfArguent = 0;
180+// int posOfBuffer = arg1;
181+// int posOfWord = arguments[arguments.length -1].length() -1;
182+// ArgumentList al = new ArgumentList(arguments, arguments.length-1, arg0.length(), arg0.length());
138183 return al;
139184 }
140185 };
@@ -142,9 +187,15 @@
142187 // ArgumentCompleter ac = new ArgumentCompleter(ad, sc);
143188 // consoleReader.addCompleter(ac);
144189 // consoleReader.addCompleter(new ConPicoCompleter());
145- CompleterOfCellNames cocn = new CompleterOfCellNames(_books.get(_currentBookName).getResolver().getCurrentSheet());
146- ArgumentCompleter ac = new ArgumentCompleter(ad, cocn);
147- consoleReader.addCompleter(ac);
190+ Book book = ctx.getCurrentBook();
191+ CompleterOfCellNames cocn = new CompleterOfCellNames(book.getResolver().getCurrentSheet());
192+// ArgumentCompleter ac = new ArgumentCompleter(ad, cocn);
193+// consoleReader.addCompleter(ac);
194+// ctx.getConsole().addCompleter(ac);
195+
196+ CompleterOfCommands cocmd = new CompleterOfCommands(ctx.get_commandStorage());
197+ ArgumentCompleter ac2 = new ArgumentCompleter(ad, cocn, cocmd);
198+ ctx.getConsole().addCompleter(ac2);
148199
149200 } catch (IOException e2) {
150201 // TODO 自動生成された catch ブロック
@@ -167,11 +218,11 @@
167218 while(true) {
168219 try {
169220
170- Book currentBook = _books.get(_currentBookName);
221+ Book currentBook = ctx.getCurrentBook();
171222 // consoleReader.readLine(makePrompt(currentBook));
172223
173224 // ヒストリからの実行か?
174- if (_nextCommand == null) {
225+ if (ctx.get_nextCommand() == null) {
175226 // ヒストリではない。
176227 // 入力がなくなったら終了
177228
@@ -180,57 +231,15 @@
180231 int p = 0;
181232 int cellSearchedPos = 0;
182233
183-// while(true) {
184-//// ch = (char) br.read();
185-// // TODO: リターンキーを押さなくてもキーを入力できるようにする
186-//// ch = (char) isr.read();
187-// ch = (char) System.in.read();
188-// if (ch == '\r') {
189-// if ((char) isr.read() == '\n') {
190-// line = linebuf.toString();
191-// break;
192-// }
193-// }
194-// if (ch != '\t') {
195-// linebuf.append(ch);
196-// // 補完機能関連の変数をリセット
197-// candiMode = false;
198-// cellSearchedPos = 0;
199-// cellNames = null;
200-// } else {
201-// if (!candiMode) {
202-// candiMode = true;
203-// // 候補文字列の取得
204-// candidate = linebuf.substring(p);
205-// // カレントシートのセル名リストが無ければ作成
206-// if (cellNames == null) {
207-// cellNames = new ArrayList<>(new TreeSet<>(_books.get(_currentBookName).getResolver().getCurrentSheet().getCellNames()));
208-// cellNamesPos = 0;
209-// }
210-// }
211-// // セル名リストの中でマッチするセル名を探す
212-// for (; cellSearchedPos < cellNames.size(); cellSearchedPos++) {
213-// String cellName = cellNames.get(cellSearchedPos);
214-// if (cellName.indexOf(candidate) == 0) {
215-// linebuf.replace(p, linebuf.length(), cellName);
216-// }
217-// }
218-// line = linebuf.toString();
219-// }
220-// System.out.println(line);
221-// }
222-
223-// if ((line = br.readLine().trim()) == null) {
224-// break;
225-// }
226234
227- if ((line = consoleReader.readLine(makePrompt(currentBook))) == null ) {
235+// if ((line = consoleReader.readLine(makePrompt(currentBook))) == null ) {
236+ if ((line = ctx.getConsole().readLine(makePrompt(currentBook))) == null ) {
228237 break;
229238 }
230239 } else {
231240 // ヒストリからの実行
232- line = _nextCommand;
233- _nextCommand = null;
241+ line = ctx.get_nextCommand();
242+ ctx.set_nextCommand(null);
234243 }
235244
236245 // 未入力だったら何もしない
@@ -244,10 +253,10 @@
244253 }
245254
246255 // ヒストリからの実行でない場合、入力内容を履歴に保持
247- if (_nextCommand == null) {
248- _cmdHistory.add(line);
249- _aggregatedHistory.remove(line);
250- _aggregatedHistory.add(line);
256+ if (ctx.get_nextCommand() == null) {
257+ ctx.get_cmdHistory().add(line);
258+ ctx.get_aggregatedHistory().remove(line);
259+ ctx.get_aggregatedHistory().add(line);
251260 }
252261
253262 // 1文字目がドットか否かでコマンドもしくはセル入力を判断
@@ -254,7 +263,7 @@
254263 if (line.charAt(0) == '.') {
255264 processCommand(line);
256265 } else {
257- _cellCommand.processCell(_books.get(_currentBookName), line);
266+ ctx.get_cellCommand().processCell(currentBook, line);
258267 }
259268
260269
@@ -277,7 +286,7 @@
277286
278287
279288 private String makePrompt(Book currentBook) {
280- return "[" + _currentBookName + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": ";
289+ return "[" + ctx.get_currentBookName() + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": ";
281290 }
282291
283292
@@ -299,16 +308,15 @@
299308 }
300309 String[] cmdArr = cmdStr.split("\\.");
301310
302- String methodName = _commandStorage.getMethodName(cmdStr.split("\\."));
311+ String methodName = ctx.get_commandStorage().getMethodName(cmdStr.split("\\."));
303312 if (methodName == null) {
304313 System.out.println("コマンド " + cmdStr + " はありません。");
305314 return;
306315 } else {
307316 try {
308-// Method m = this.getClass().getMethod(methodName);
309- Method m = this.getClass().getDeclaredMethod(methodName, String.class);
317+ Method m = this.getClass().getDeclaredMethod(methodName, String.class, Context.class);
310318 m.setAccessible(true);
311- m.invoke(this, argStr);
319+ m.invoke(this, argStr, ctx);
312320 } catch (NoSuchMethodException e) {
313321 System.out.println("コマンド " + cmdStr + "に対応するメソッド " + methodName + " が定義されていません。");
314322 return;
@@ -332,15 +340,15 @@
332340
333341 }
334342
335- private void doQuit(String argStr) {
343+ private void doQuit(String argStr, Context ctx) {
336344 _needQuit = true;
337345 }
338346
339- private void doHelp(String argStr) {
347+ private void doHelp(String argStr, Context ctx) {
340348 if (argStr.isEmpty()){
341- System.out.println(_commandStorage.getHelp());
349+ System.out.println(ctx.get_commandStorage().getHelp());
342350 } else {
343- String helpString = _commandStorage.getHelp(argStr);
351+ String helpString = ctx.get_commandStorage().getHelp(argStr);
344352 if (helpString != null) {
345353 System.out.println(helpString);
346354 } else {
@@ -349,61 +357,60 @@
349357 }
350358 }
351359
352- private void doHistory(String argStr) {
353- StringBuilder sb = new StringBuilder();
354- int cnt = 1;
355- for (String s : _cmdHistory) {
356- sb.append(cnt).append("\t").append(s).append("\n");
357- cnt++;
360+ private void doHistory(String argStr, Context ctx) {
361+ History history = ctx.getConsole().getHistory();
362+
363+ for (Entry e : history) {
364+ System.out.println(" " + Integer.toString(e.index()) + " " + e.value());
358365 }
359- if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
360- sb.deleteCharAt(sb.length() - 1);
361- }
362- System.out.println(sb);
363366 }
364367
365- private void doExecHistory(String argStr) {
368+ private void doExecHistory(String argStr, Context ctx) {
366369 int reqHistNo = 0;
367370 try {
368371 reqHistNo = Integer.parseInt(argStr);
369372 } catch (Exception e) {
373+ // do nothing
370374 return;
371375 }
376+ History history = ctx.getConsole().getHistory();
372377
373- this._nextCommand = this._cmdHistory.get(reqHistNo - 1);
374- System.out.println(_nextCommand);
378+ ctx.set_nextCommand(history.get(reqHistNo).toString());
379+ if (ctx.get_nextCommand() != null) {
380+ System.out.println(ctx.get_nextCommand());
381+ }
375382
376383 }
377384
378- private void doCellList(String argStr) {
379- Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
380- _cellCommand.listCells(sheet);
381- _groupCommand.listGroups(sheet);
382- _tableCommand.listTables(sheet);
385+ private void doCellList(String argStr, Context ctx) {
386+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
387+ ctx.get_cellCommand().listCells(sheet);
388+ ctx.get_groupCommand().listGroups(sheet);
389+ ctx.get_tableCommand().listTables(sheet);
383390 return;
384391 }
385392
386- private void doBookAdd(String argStr) {
393+ private void doBookAdd(String argStr, Context ctx) {
387394 Book newBook = new Book(argStr);
388395 newBook.addSheet("sheet1");
389- _books.put(newBook.getName(), newBook);
396+ ctx.get_books().put(newBook.getName(), newBook);
390397 }
391398
392- private void doBookCurrent(String argStr) {
399+ private void doBookCurrent(String argStr, Context ctx) {
393400 // カレントブックの変更
394- if (_books.containsKey(argStr)) {
395- this._currentBookName = argStr;
401+ if (ctx.get_books().containsKey(argStr)) {
402+ ctx.set_currentBookName(argStr);
396403 }
397404 }
398405
399- private void doBookList(String argStr) {
400- for (String bookName: new TreeSet<String>(_books.keySet())) {
406+ private void doBookList(String argStr, Context ctx) {
407+ for (String bookName: new TreeSet<String>(ctx.get_books().keySet())) {
401408 System.out.println(bookName);
402409 }
403410 }
404411
405- private void doBookFunctions(String argStr) {
406- String[] functionNames = _books.get(_currentBookName).getFunctionNames();
412+ private void doBookFunctions(String argStr, Context ctx) {
413+ String[] functionNames = ctx.getCurrentBook().getFunctionNames();
407414 Arrays.sort(functionNames);
408415 for (String funcName: Arrays.asList(functionNames)) {
409416 System.out.println(funcName);
@@ -410,15 +417,15 @@
410417 }
411418 }
412419
413- private void doBookLoad(String argStr) {
420+ private void doBookLoad(String argStr, Context ctx) {
414421 String filePath = argStr;
415- Book book = _books.get(_currentBookName);
422+ Book book = ctx.getCurrentBook();
416423 load(book, filePath);
417424 }
418425
419- private void doBookSave(String argStr) {
426+ private void doBookSave(String argStr, Context ctx) {
420427 String filePath = argStr;
421- Book book = _books.get(_currentBookName);
428+ Book book = ctx.getCurrentBook();
422429 SimpleWriter sw = new SimpleWriter(new ConcurrentBookWrapper(book));
423430 BufferedWriter w = null;
424431 try {
@@ -460,16 +467,86 @@
460467 }
461468
462469
463- private void doSheetList(String argStr) {
464- BookCommand.listSheets(_books.get(_currentBookName), System.out);
470+ private void doSheetList(String argStr, Context ctx) {
471+ BookCommand.listSheets(ctx.getCurrentBook(), System.out);
465472 }
466473
467- private void doSheetAdd(String argStr) {
468- _books.get(_currentBookName).addSheet(argStr);
474+ private void doSheetAdd(String argStr, Context ctx) {
475+ ctx.getCurrentBook().addSheet(argStr);
469476 }
470477
471- private void doSheetCurrent(String argStr) {
472- Book book = _books.get(_currentBookName);
478+ private void doSheetCurrent(String argStr, Context ctx) {
479+ Book book = ctx.getCurrentBook();
473480 book.getResolver().setCurrentSheet(book.getSheet(argStr));
474481 }
482+
483+ private void doGroupAdd(String argStr, Context ctx) {
484+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
485+ if (sheet.groupExists(argStr)) {
486+ return;
487+ }
488+
489+ try {
490+ sheet.addGroup(argStr);
491+ } catch (Exception e) {
492+ e.printStackTrace(ctx.getOut());
493+ }
494+ }
495+
496+ private void doGroupList(String argStr, Context ctx) {
497+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
498+ for (Group g : sheet.getGroups()) {
499+ ctx.getOut().println(g.getName());
500+ }
501+ }
502+
503+ private void doGroupCellsAdd(String argStr, Context ctx) {
504+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
505+ String[] args = argStr.split(" +");
506+ String groupName = args[0];
507+ if (!sheet.groupExists(groupName)) {
508+ ctx.getOut().println("グループ " + groupName + " は存在しません。");
509+ return;
510+ }
511+ for (int i = 1; i < args.length; i++) {
512+ String cellName = args[i];
513+ if (!sheet.cellExists(cellName)) {
514+ ctx.getOut().println("セル " + cellName + " は存在しません。");
515+ return;
516+ }
517+ sheet.getGroup(groupName).addCell(cellName);
518+ }
519+ }
520+
521+ private void doGroupListCells(String argStr, Context ctx) {
522+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
523+ if (!sheet.groupExists(argStr)) {
524+ ctx.getOut().println("グループ " + argStr + " は存在しません。");
525+ return;
526+ }
527+ Group g = sheet.getGroup(argStr);
528+ for (Cell c: g.getCells()) {
529+ ctx.getOut().println(c.getName());
530+ }
531+ }
532+
533+ private void doGroupCellsRemove(String argStr, Context ctx) {
534+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
535+ String[] args = argStr.split(" +");
536+ String groupName = args[0];
537+ if (!sheet.groupExists(groupName)) {
538+ ctx.getOut().println("グループ " + groupName + " は存在しません。");
539+ return;
540+ }
541+ Group g = sheet.getGroup(groupName);
542+ for (int i = 1; i < args.length; i++) {
543+ String cellName = args[i];
544+ sheet.getGroup(groupName).removeCell(cellName);
545+ }
546+ }
547+
548+ private void doGroupDelete(String argStr, Context ctx) {
549+ Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet();
550+ sheet.deleteGroup(argStr);
551+ }
475552 }
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/Context.java (nonexistent)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/Context.java (revision 18)
@@ -0,0 +1,148 @@
1+package com.nissy_ki_chi.ConsolePicocalc;
2+
3+import java.io.InputStream;
4+import java.io.PrintStream;
5+import java.util.HashMap;
6+import java.util.LinkedList;
7+import java.util.Map;
8+
9+import com.nissy_ki_chi.jpicosheet.core.Book;
10+
11+import jline.console.ConsoleReader;
12+
13+public class Context {
14+
15+ private Map<String, Book> _books = new HashMap<String, Book>();
16+ private String _currentBookName = null;
17+
18+ private BookCommand _bookCommand = new BookCommand();
19+ private SheetCommand _sheetCommand = new SheetCommand();
20+ private CellCommand _cellCommand = new CellCommand();
21+ private GroupCommand _groupCommand = new GroupCommand();
22+ private TableCommand _tableCommand = new TableCommand();
23+
24+ private LinkedList<String> _cmdHistory = new LinkedList<>();
25+ private LinkedList<String> _aggregatedHistory = new LinkedList<>();
26+ private String _nextCommand = null;
27+
28+ private CommandFragment _commandStorage = new CommandFragment(".");
29+
30+ private InputStream in = null;
31+ private PrintStream out = null;
32+
33+ private static String DEFAULT_BOOK_NAME = "myBook";
34+ private static String DEFAULT_SHEET_NAME = "Sheet1";
35+
36+ public Context() {};
37+
38+ public ConsoleReader getConsole() {
39+ return console;
40+ }
41+
42+ public void setConsole(ConsoleReader console) {
43+ this.console = console;
44+ }
45+ ConsoleReader console;
46+
47+ public Map<String, Book> get_books() {
48+ return _books;
49+ }
50+
51+ public Book getCurrentBook(){
52+ return _books.get(_currentBookName);
53+ }
54+
55+
56+ public String get_currentBookName() {
57+ return _currentBookName;
58+ }
59+ public BookCommand get_bookCommand() {
60+ return _bookCommand;
61+ }
62+ public SheetCommand get_sheetCommand() {
63+ return _sheetCommand;
64+ }
65+ public CellCommand get_cellCommand() {
66+ return _cellCommand;
67+ }
68+ public GroupCommand get_groupCommand() {
69+ return _groupCommand;
70+ }
71+ public TableCommand get_tableCommand() {
72+ return _tableCommand;
73+ }
74+ public LinkedList<String> get_cmdHistory() {
75+ return _cmdHistory;
76+ }
77+ public LinkedList<String> get_aggregatedHistory() {
78+ return _aggregatedHistory;
79+ }
80+ public String get_nextCommand() {
81+ return _nextCommand;
82+ }
83+ public CommandFragment get_commandStorage() {
84+ return _commandStorage;
85+ }
86+ public static String getDEFAULT_BOOK_NAME() {
87+ return DEFAULT_BOOK_NAME;
88+ }
89+ public static String getDEFAULT_SHEET_NAME() {
90+ return DEFAULT_SHEET_NAME;
91+ }
92+ public void set_books(Map<String, Book> _books) {
93+ this._books = _books;
94+ }
95+ public void set_currentBookName(String _currentBookName) {
96+ this._currentBookName = _currentBookName;
97+ }
98+ public void set_bookCommand(BookCommand _bookCommand) {
99+ this._bookCommand = _bookCommand;
100+ }
101+ public void set_sheetCommand(SheetCommand _sheetCommand) {
102+ this._sheetCommand = _sheetCommand;
103+ }
104+ public void set_cellCommand(CellCommand _cellCommand) {
105+ this._cellCommand = _cellCommand;
106+ }
107+ public void set_groupCommand(GroupCommand _groupCommand) {
108+ this._groupCommand = _groupCommand;
109+ }
110+ public void set_tableCommand(TableCommand _tableCommand) {
111+ this._tableCommand = _tableCommand;
112+ }
113+ public void set_cmdHistory(LinkedList<String> _cmdHistory) {
114+ this._cmdHistory = _cmdHistory;
115+ }
116+ public void set_aggregatedHistory(LinkedList<String> _aggregatedHistory) {
117+ this._aggregatedHistory = _aggregatedHistory;
118+ }
119+ public void set_nextCommand(String _nextCommand) {
120+ this._nextCommand = _nextCommand;
121+ }
122+ public void set_commandStorage(CommandFragment _commandStorage) {
123+ this._commandStorage = _commandStorage;
124+ }
125+ public static void setDEFAULT_BOOK_NAME(String dEFAULT_BOOK_NAME) {
126+ DEFAULT_BOOK_NAME = dEFAULT_BOOK_NAME;
127+ }
128+ public static void setDEFAULT_SHEET_NAME(String dEFAULT_SHEET_NAME) {
129+ DEFAULT_SHEET_NAME = dEFAULT_SHEET_NAME;
130+ }
131+
132+ public InputStream getIn() {
133+ return in;
134+ }
135+
136+ public void setIn(InputStream is) {
137+ this.in = is;
138+ }
139+
140+ public PrintStream getOut() {
141+ return out;
142+ }
143+
144+ public void setOut(PrintStream os) {
145+ this.out = os;
146+ }
147+
148+}