-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexscroll.java
More file actions
425 lines (368 loc) · 19.3 KB
/
indexscroll.java
File metadata and controls
425 lines (368 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
*
* WDBM/extools Database Development Project
* Rewritten In Java By Zen Harris 2015 househarris@b5.net
* From the original code in Perl
* Copyright (C) 1993 Michael Brown mbrown@scorch.hna.com.au
*
*
* 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 1, 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
package org.househarris.extools;
import com.googlecode.lanterna.SGR;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TextColor;
// import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType;
//import com.googlecode.lanterna.terminal.Terminal;
// import com.googlecode.lanterna.terminal.TerminalSize;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
//import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
//import java.util.PriorityQueue;
//import java.util.Queue;
import java.util.Stack;
//import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
//import java.util.concurrent.Semaphore;
//import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/// import javax.swing.JFrame;
import org.househarris.extools.wdbm.TerminalWindow;
/**
*
* @author harris
*/
public class indexscroll implements extools {
public int ListScrollsIndex = 0;
public String IndexScrollName;
public boolean ConnectedForm = false;
public ResultSet Results;
public int ScreenCurrentRow;
public int ResultsCurrentRow;
public int ListScreenTopLine = 0;
public int ListScreenLength = 0;
private final String ScrollPrompt = "[Enter]Select [S]QL Query [ARROWS]ScrollUP/DN [Home]Exit";
public wdbm AttachedWDBM;
public Statement stmt;
public String CurrentSQLQuery;
public PreparedStatement CurrentCompiledSQLStatement;
public List<String> SQLQueryHistory = new ArrayList();
public searchAtom CurrentSearchAtom;
public Stack<searchAtom> SearchAtomStack = new Stack();
public indexscroll(String ScrollName ,String SQLQuery,wdbm WDBMAttach,int... Dimensions) throws SQLException,IOException {
System.err.println("Making New Index Scroll :" + ScrollName);
if(Dimensions.length > 0){
ListScreenTopLine = Dimensions[0];
ListScreenLength = WDBMAttach.WindowStack.peek().rawTerminal.getTerminalSize().getRows()- ListScreenTopLine -3;
}
if(Dimensions.length > 1) ListScreenLength = Dimensions[1];
SearchAtomStack.push(CurrentSearchAtom = new searchAtom(SQLQuery, WDBMAttach));
Results = SearchAtomStack.peek().AtomicResultSet;
if (!Results.first()) System.err.println("This returned nothing " + SQLQuery); // Empirical kudge ????
ScreenCurrentRow = 0;
ResultsCurrentRow = Results.getRow();
AttachedWDBM = WDBMAttach;
CurrentSQLQuery = SQLQuery;
IndexScrollName = ScrollName;
ListScrollsIndex = AttachedWDBM.ScrollStack.size();
System.err.println(IndexScrollName + " " + ListScrollsIndex + " " + CurrentSQLQuery );
}
//private List<String> Substitute = new ArrayList();
private List<String> FieldNames2ValuesSubstitute(List<String> FieldNameList,ResultSet LocalResult) throws SQLException {
List<String> Substitute = new ArrayList();
// Substitute.clear();
for (String FieldName : FieldNameList) {
boolean add = Substitute.add(LocalResult.getString(FieldName));
}
// String[] returnArray = Substitute.toArray();
return Substitute;
}
public void ReDrawScroll(searchAtom... AttachedSearchAtom) throws SQLException,InterruptedException,IOException {
TerminalWindow UseWindow;
if(AttachedSearchAtom.length>0) UseWindow = AttachedSearchAtom[0].AtomicOutTerminal;
else UseWindow = CurrentSearchAtom.AtomicOutTerminal;
TerminalSize Tsize = UseWindow.TerminalSize();
int SaveResultRow = CurrentSearchAtom.AtomicResultSet.getRow();
if (SaveResultRow < 1) AttachedWDBM.DisplayError("Something Wrong ReDrawScroll getRow" + SaveResultRow,UseWindow); /////Diagnostic
int startrow = SaveResultRow - ScreenCurrentRow;
if (startrow < 1) {
startrow = 1;
ScreenCurrentRow = SaveResultRow -1;
}
int iter;
for (iter = 0; (ListScreenLength==0 || iter < ListScreenLength) && ListScreenTopLine+iter + 4 <= Tsize.getRows() &&
CurrentSearchAtom.AtomicResultSet.absolute(startrow + iter); iter++) {
UseWindow.DisplayString(0, ListScreenTopLine+iter, String.format(AttachedWDBM.DefaultScrollFormat,
FieldNames2ValuesSubstitute(AttachedWDBM.DefaultScrollFields,CurrentSearchAtom.AtomicResultSet).toArray()));
}
iter--;
while (iter++ < ListScreenLength-1) UseWindow.DisplayString(0, ListScreenTopLine+iter, BLANK);
CurrentSearchAtom.AtomicResultSet.absolute(SaveResultRow);
}
public void IlluminateCurrentRow() throws SQLException,IOException {
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setForegroundColor(TextColor.ANSI.BLACK);
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setBackgroundColor(TextColor.ANSI.WHITE);
// CurrentSearchAtom.AtomicOutTerminal.screenHandle.putString(0, ListScreenTopLine + ScreenCurrentRow,
CurrentSearchAtom.AtomicOutTerminal.DisplayString(0, ListScreenTopLine + ScreenCurrentRow,
String.format(AttachedWDBM.DefaultScrollFormat, FieldNames2ValuesSubstitute(AttachedWDBM.DefaultScrollFields,CurrentSearchAtom.AtomicResultSet).toArray()),
// TextColor.ANSI.BLACK, TextColor.ANSI.WHITE);
SGR.BOLD);
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setForegroundColor(TextColor.ANSI.WHITE);
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setBackgroundColor(TextColor.ANSI.BLACK);
CurrentSearchAtom.AtomicOutTerminal.Refresh();
}
/**
*
* @throws SQLException
*/
public void DeEmphasiseCurrentRow() throws SQLException, IOException {
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setForegroundColor(TextColor.ANSI.WHITE);
CurrentSearchAtom.AtomicOutTerminal.screenWriter.setBackgroundColor(TextColor.ANSI.BLACK);
CurrentSearchAtom.AtomicOutTerminal.screenWriter.putString(0, ListScreenTopLine + ScreenCurrentRow,
String.format(AttachedWDBM.DefaultScrollFormat,
FieldNames2ValuesSubstitute(AttachedWDBM.DefaultScrollFields,CurrentSearchAtom.AtomicResultSet).toArray()));
// TextColor.ANSI.WHITE, TextColor.ANSI.BLACK);
CurrentSearchAtom.AtomicOutTerminal.Refresh();
}
public Thread SQLQueryThread;
private final BlockingQueue<searchAtom> SQLQueryQueue = new LinkedBlockingQueue();
public searchAtom returnNewSearchAtom(String SQLStatement,wdbm Wdbm) throws SQLException {
return new searchAtom(SQLStatement, Wdbm);
}
@Override
public indexscroll WithTheIndexScroll(String ScrollName) throws SQLException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public class searchAtom{
Connection AtomicSQLMainPipe;
String AtomicSQL;
TerminalWindow AtomicOutTerminal;
ResultSet AtomicResultSet;
Statement AtomicStatement;
PreparedStatement AtomicCompiledStatement;
public searchAtom (String SQLStatement,wdbm Wdbm) throws SQLException {
AtomicSQLMainPipe = Wdbm.SQLconnection; //SQLMainPipe;
AtomicSQL = SQLStatement;
AtomicOutTerminal = Wdbm.WindowStack.peek(); //Terminal;
// AtomicResultSet = Results;
AtomicCompiledStatement = AtomicSQLMainPipe.prepareStatement(SQLStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
AtomicResultSet = AtomicCompiledStatement.executeQuery();
if (!AtomicResultSet.first()) {
throw new SQLException("searchAtom Error Condition -> no find first");
}
}
/**
* Recycles Current Search Atom giving it a new SQL query and optionally
* a new Terminal Window to send it's output.
*
* @param NewSQLQuery
* @param ToTerm Optional TerminalWindow to send all output
* @throws SQLException
* @throws InterruptedException;
* @return New SearchAtom if called with no currentSearchAtom.
*/
public searchAtom Recycle(String NewSQLQuery, TerminalWindow... ToTerm) throws SQLException, InterruptedException {
TerminalWindow UseWindow;
if (ToTerm.length > 0)
UseWindow = ToTerm[0];
else UseWindow = AtomicOutTerminal;
if (NewSQLQuery.equals(AtomicSQL)) return this;
if (SQLQueryThread != null && SQLQueryThread.isAlive()) {
if (SQLQueryQueue.size() > 1) {
SQLQueryThread.interrupt();
}
SQLQueryQueue.clear(); //overrun mittigated
// SQLQueryQueue.add(SearchAtom = new searchAtom(NewSQLQuery,UseWindow,AttachedWDBM.SQLconnection));
AtomicSQL = NewSQLQuery;
if (ToTerm.length > 0) {
AtomicOutTerminal = ToTerm[0];
}
SQLQueryQueue.add(this); // new searchAtom(NewSQLQuery,UseWindow,AttachedWDBM.SQLconnection));
return this;
}
AtomicSQL = NewSQLQuery;
boolean add = SQLQueryQueue.add(this); // new searchAtom(NewSQLQuery,UseWindow,AttachedWDBM.SQLconnection));
SQLQueryThread = new Thread(new QuantumResearchDaemon());
UseWindow.ThreadPool.add(SQLQueryThread.getId());
SQLQueryThread.start();
return this;
}
}
public class QuantumResearchDaemon implements Runnable {
@Override
public void run() {
// ResultSet LocalResults;
TerminalWindow Terminal = null;
try {
do {
searchAtom QueuedParameter = SQLQueryQueue.take();
Terminal = QueuedParameter.AtomicOutTerminal;
// if (!QueuedParameter.AtomicSQL.equals(CurrentSQLQuery)) {
AttachedWDBM.DisplayStatusLine("SQL Data Transfer Taking Place");
Terminal.Refresh();
CurrentCompiledSQLStatement = AttachedWDBM.SQLconnection.prepareStatement(QueuedParameter.AtomicSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
QueuedParameter.AtomicCompiledStatement = CurrentCompiledSQLStatement;
QueuedParameter.AtomicResultSet = CurrentCompiledSQLStatement.executeQuery();
if (QueuedParameter.AtomicResultSet.first()) {
// Results = QueuedParameter.AtomicResultSet;
ScreenCurrentRow = 0;
ResultsCurrentRow = QueuedParameter.AtomicResultSet.getRow();
// AttachedWDBM.scrn.clear();
ReDrawScroll(QueuedParameter);
SQLQueryHistory.add(CurrentSQLQuery);
CurrentSQLQuery = QueuedParameter.AtomicSQL;
}
// }
AttachedWDBM.DisplayStatusLine("");
// AttachedWDBM.screenHandle.refresh();
} while (true);
} catch (InterruptedException IEx) {
IEx.printStackTrace();
} catch (IOException IOx) {
IOx.printStackTrace();
} catch (NoSuchElementException | SQLException | NullPointerException ex) {
try {
// SQLQueryQueue.clear();
AttachedWDBM.DisplayError(ex.getClass().getName() + ": " + ex.getMessage() + "Zen",Terminal);
} catch (SQLException ex1) {
Logger.getLogger(indexscroll.class.getName()).log(Level.SEVERE, null, ex1);
} catch (IOException Iox) {
}
} catch (IllegalMonitorStateException ex) {
ex.printStackTrace();
// AttachedWDBM.DisplayError(ex.getClass().getName() + ": " + ex.getMessage() + "Zen");
}finally {
}
}
}
/**
*
* @param SQLQuery
* @throws SQLException
*/
public void ExecuteSQLQuery(String SQLQuery,TerminalWindow... ToTerm) throws SQLException,InterruptedException,IOException {
// ResultSet LocalResults;
TerminalWindow UseWindow;
if (ToTerm.length>0) UseWindow = ToTerm[0];
else UseWindow = CurrentSearchAtom.AtomicOutTerminal;
try {
AttachedWDBM.TextEditor.LineEditorBuffer = CurrentSQLQuery;
AttachedWDBM.TextEditor.LineEditorPosition = CurrentSQLQuery.length();
String LocalString = AttachedWDBM.PromptForString("->",UseWindow);
// if (AttachedWDBM.TextEditor.LineEditorReturnKey.getKind() != Key.Kind.Escape) {
if (AttachedWDBM.TextEditor.LineEditorReturnKey.getKeyType() != KeyType.Escape) {
Results = CurrentSearchAtom.Recycle(LocalString).AtomicResultSet;
// Results = CurrentSearchAtom.AtomicResultSet;
}
} catch (SQLException ex) {
AttachedWDBM.DisplayError(ex.getClass().getName() + ": " + ex.getMessage() + " SQLState " + ex.getSQLState(),UseWindow);
ex.printStackTrace();
} finally {
AttachedWDBM.DisplayError("",UseWindow);
AttachedWDBM.DisplayStatusLine("");
UseWindow.Refresh();
}
}
/*
public void ExecuteHTMLQuery (String HTMLQuery,TerminalWindow ToTerm) throws SQLException,InterruptedException,IOException {
// AttachedWDBM.TextEditor.LineEditorBuffer = CurrentSQLQuery;
// AttachedWDBM.TextEditor.LineEditorPosition = CurrentSQLQuery.length();
String LocalString = AttachedWDBM.PromptForString("->", ToTerm);
if (AttachedWDBM.TextEditor.LineEditorReturnKey.getKeyType() != KeyType.Escape) {
browser = new SwingBrowser();
JFrame main = browser.getMainWindow();
main.setSize(1100, 850);
main.setVisible(true);
browser.displayURL(LocalString);
}
}
*/
// public Key ActivateScroll(TerminalWindow... AttachedWindow) throws SQLException,InterruptedException{
public KeyStroke ActivateScroll(TerminalWindow... AttachedWindow) throws SQLException,InterruptedException,IOException {
KeyStroke KeyReturn;
TerminalSize Tsize;
TerminalWindow UseWindow;
if (AttachedWindow.length>0) UseWindow = AttachedWindow[0];
else UseWindow = CurrentSearchAtom.AtomicOutTerminal;
ReDrawScroll();
while (true) {
UseWindow.Refresh();
Tsize = UseWindow.TerminalSize();
ResultsCurrentRow = CurrentSearchAtom.AtomicResultSet.getRow();
IlluminateCurrentRow();
if(ConnectedForm) AttachedWDBM.FormDisplay(CurrentSearchAtom.AtomicResultSet,CurrentSearchAtom.AtomicOutTerminal);
UseWindow.Refresh();
// if ((KeyReturn = AttachedWDBM.KeyInput(UseWindow,ScrollPrompt)).getKeyType() == KeyType.ReverseTab) {
if ((KeyReturn = AttachedWDBM.KeyInput(UseWindow,ScrollPrompt)).getKeyType() == KeyType.ReverseTab) {
DeEmphasiseCurrentRow();
return KeyReturn;
} else if (KeyReturn.getKeyType() == KeyType.ArrowDown && !CurrentSearchAtom.AtomicResultSet.isLast()) {
if ((ListScreenLength == 0 || ScreenCurrentRow + 1 < ListScreenLength) && ListScreenTopLine + ScreenCurrentRow + 4 < Tsize.getRows()) {
DeEmphasiseCurrentRow();
ScreenCurrentRow++;
CurrentSearchAtom.AtomicResultSet.next();
} else {
CurrentSearchAtom.AtomicResultSet.next();
ReDrawScroll();
}
} else if (KeyReturn.getKeyType() == KeyType.ArrowUp && !CurrentSearchAtom.AtomicResultSet.isFirst()) {
if (ScreenCurrentRow > 0) {
DeEmphasiseCurrentRow();
ScreenCurrentRow--;
CurrentSearchAtom.AtomicResultSet.previous();
} else {
CurrentSearchAtom.AtomicResultSet.previous();
ReDrawScroll();
}
} else if (KeyReturn.getKeyType() == KeyType.PageDown && !CurrentSearchAtom.AtomicResultSet.isLast()) {
CurrentSearchAtom.AtomicResultSet.relative(ListScreenLength-1);
if(CurrentSearchAtom.AtomicResultSet.isAfterLast()) CurrentSearchAtom.AtomicResultSet.last();
ReDrawScroll();
} else if (KeyReturn.getKeyType() == KeyType.PageUp) {
if(CurrentSearchAtom.AtomicResultSet.getRow()-(ListScreenLength) > ListScreenLength) CurrentSearchAtom.AtomicResultSet.relative(1-ListScreenLength);
else CurrentSearchAtom.AtomicResultSet.absolute(ScreenCurrentRow+1);
// else Results.first();
ReDrawScroll();
} else if (KeyReturn.getKeyType() == KeyType.End) {
CurrentSearchAtom.AtomicResultSet.last();
ReDrawScroll();
} else if (KeyReturn.getKeyType() == KeyType.Home) {
CurrentSearchAtom.AtomicResultSet.first();
ReDrawScroll();
} else if (KeyReturn.getKeyType() == KeyType.Character) {
if (KeyReturn.getCharacter() == 's') {
ExecuteSQLQuery(CurrentSQLQuery);
} else if (KeyReturn.getCharacter() == 'h') {
// ExecuteHTMLQuery("", UseWindow);
} else return KeyReturn;
} else if (KeyReturn.getKeyType() == KeyType.Enter || KeyReturn.getKeyType() == KeyType.Escape) {
DeEmphasiseCurrentRow();
return KeyReturn;
}
}
}
}