From d85b4139daff6c21bbeefe28eb9a2236f79509c9 Mon Sep 17 00:00:00 2001 From: sylvain Date: Wed, 22 Apr 2020 22:58:09 -0400 Subject: [PATCH 1/5] K.java - improve toString for kdict, klist and ksymbolvector --- src/studio/kdb/K.java | 69 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index c0aa54ea..d6031261 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -8,11 +8,8 @@ import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; +import java.util.function.ToLongFunction; public class K { private static SimpleDateFormat formatter = new SimpleDateFormat(); @@ -584,7 +581,7 @@ public void serialise(OutputStream o) throws IOException { } } - public static class KLong extends KBase implements ToDouble { + public static class KLong extends KBase implements ToDouble, ToLongFunction { public String getDataType() { return "Long"; } @@ -595,6 +592,11 @@ public double toDouble() { return j; } + @Override + public long applyAsLong(KLong o) { + return j; + } + public KLong(long j) { this.j = j; type = -7; @@ -976,7 +978,7 @@ public void upsert(K.Dict upd) { public void toString(LimitedWriter w, boolean showType) throws IOException { boolean useBrackets = getAttr() != 0 || x instanceof Flip; - super.toString(w, showType); + w.write(super.toString(showType)); if (useBrackets) w.write("("); x.toString(w, showType); @@ -985,6 +987,11 @@ public void toString(LimitedWriter w, boolean showType) throws IOException { w.write("!"); y.toString(w, showType); } + + @Override + public String toString(boolean showType) { + return "(" + x.toString(showType) + ")!" + y.toString(showType); + } } public static class Flip extends KBase { @@ -1353,6 +1360,25 @@ public void toString(LimitedWriter w, boolean showType) throws IOException { if (getLength() != 1) w.write(")"); } + + @Override + public String toString(boolean showType) { + StringBuilder sb = new StringBuilder(); + sb.append(super.toString(showType)); + if (getLength() == 1) { + sb.append(enlist); + } else { + sb.append("("); + } + for (int i = 0; i < getLength(); i++) { + if (i > 0) + sb.append(";"); + sb.append(at(i).toString(showType)); + } + if (getLength() != 1) + sb.append(")"); + return sb.toString(); + } } public static class KDoubleVector extends KBaseVector { @@ -1950,13 +1976,32 @@ public KBase at(int i) { public void toString(LimitedWriter w, boolean showType) throws IOException { w.write(super.toString(showType)); - if (getLength() == 0) - w.write("0#`"); - else if (getLength() == 1) - w.write(enlist); + toString(w); + } + + private void toString(Appendable appendable) throws IOException { + if (getLength() == 0) { + appendable.append("0#`"); + return; + } + + if (getLength() == 1) { + appendable.append(enlist); + } for (int i = 0; i < getLength(); i++) - w.write("`" + Array.get(array, i)); + appendable.append("`").append(Array.get(array, i).toString()); + } + + @Override + public String toString(boolean showType) { + try { + StringBuilder sb = new StringBuilder(); + toString(sb); + return sb.toString(); + } catch (IOException e) { + return super.toString(showType); + } } } From 37d3f2db53658d0919a09c920660fb371b860def Mon Sep 17 00:00:00 2001 From: sylvain Date: Tue, 5 May 2020 19:12:34 -0400 Subject: [PATCH 2/5] LimitedWriter - Override write(string, int, int) instead of write(string) to cover more use case (Appendable.append e.g) --- src/studio/kdb/LimitedWriter.java | 16 +- src/studio/ui/StudioPanel.java | 4757 +++++++++++++++-------------- 2 files changed, 2389 insertions(+), 2384 deletions(-) diff --git a/src/studio/kdb/LimitedWriter.java b/src/studio/kdb/LimitedWriter.java index c902a5fb..dd641e2a 100755 --- a/src/studio/kdb/LimitedWriter.java +++ b/src/studio/kdb/LimitedWriter.java @@ -4,7 +4,9 @@ import java.io.IOException; public class LimitedWriter extends CharArrayWriter { - private int limit; + private static final String TOO_LONG = " ... "; + + private final int limit; public static class LimitException extends RuntimeException { } @@ -15,18 +17,20 @@ public LimitedWriter(int limit) { public void write(char c) throws IOException { if ((1 + size()) > limit) { - super.write(" ... "); + super.write(TOO_LONG); throw new LimitException(); } super.write(c); } - public void write(String s) throws IOException { + @Override + public void write(String s, int off, int len) { if ((size() + s.length()) > limit) { - super.write(s.substring(0,limit - size())); - super.write(" ... "); + String substring = s.substring(0, limit - size()); + super.write(substring, 0, substring.length()); + super.write(TOO_LONG, 0, TOO_LONG.length()); throw new LimitException(); } - super.write(s); + super.write(s, off, len); } } diff --git a/src/studio/ui/StudioPanel.java b/src/studio/ui/StudioPanel.java index d8e36d0e..c00c6890 100755 --- a/src/studio/ui/StudioPanel.java +++ b/src/studio/ui/StudioPanel.java @@ -1,2378 +1,2379 @@ -package studio.ui; - -import java.awt.*; -import java.awt.event.*; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.io.*; -import java.util.*; -import java.util.List; -import javax.swing.*; -import static javax.swing.JSplitPane.VERTICAL_SPLIT; -import static studio.ui.EscapeDialog.DialogResult.ACCEPTED; -import static studio.ui.EscapeDialog.DialogResult.CANCELLED; - -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.UndoableEditEvent; -import javax.swing.filechooser.FileFilter; -import javax.swing.filechooser.FileView; -import javax.swing.plaf.basic.BasicSplitPaneUI; -import javax.swing.table.TableModel; -import javax.swing.text.*; -import javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; -import javax.swing.undo.UndoManager; -import kx.c; -import org.netbeans.editor.*; -import org.netbeans.editor.Utilities; -import studio.core.Credentials; -import studio.kdb.ListModel; -import studio.qeditor.QKit; -import org.netbeans.editor.ext.ExtKit; -import org.netbeans.editor.ext.ExtSettingsInitializer; -import studio.qeditor.QSettingsInitializer; -import studio.kdb.*; -import studio.utils.BrowserLaunch; -import studio.utils.OSXAdapter; -import studio.utils.SwingWorker; - -public class StudioPanel extends JPanel implements Observer,WindowListener { - static { - // Register us - LocaleSupport.addLocalizer(new Impl("org.netbeans.editor.Bundle")); - - Settings.addInitializer(new BaseSettingsInitializer(),Settings.CORE_LEVEL); - Settings.addInitializer(new ExtSettingsInitializer(),Settings.CORE_LEVEL); - - QKit editorKit = new QKit(); - JEditorPane.registerEditorKitForContentType(editorKit.getContentType(), - editorKit.getClass().getName()); - Settings.addInitializer(new QSettingsInitializer()); - Settings.reset(); - } - - private JComboBox comboServer; - private JTextField txtServer; - private JTable table; - private String exportFilename; - private String lastQuery = null; - private JMenuBar menubar; - private JToolBar toolbar; - private JEditorPane textArea; - private JSplitPane splitpane; - private JTabbedPane tabbedPane; - private Font font = null; - private UserAction arrangeAllAction; - private UserAction closeFileAction; - private UserAction newFileAction; - private UserAction openFileAction; - private UserAction openInExcel; - private UserAction codeKxComAction; - private UserAction serverListAction; - private UserAction openFileInNewWindowAction; - private UserAction saveFileAction; - private UserAction saveAsFileAction; - private UserAction exportAction; - private UserAction chartAction; - private ActionFactory.UndoAction undoAction; - private ActionFactory.RedoAction redoAction; - private BaseKit.CutAction cutAction; - private BaseKit.CopyAction copyAction; - private BaseKit.PasteAction pasteAction; - private BaseKit.SelectAllAction selectAllAction; - private Action findAction; - private Action replaceAction; - private UserAction stopAction; - private UserAction executeAction; - private UserAction executeCurrentLineAction; - private UserAction refreshAction; - private UserAction aboutAction; - private UserAction exitAction; - private UserAction settingsAction; - private UserAction toggleDividerOrientationAction; - private UserAction minMaxDividerAction; - private UserAction editServerAction; - private UserAction addServerAction; - private UserAction removeServerAction; - private static int scriptNumber = 0; - private static int myScriptNumber; - private JFrame frame; - public static java.util.List windowList = Collections.synchronizedList(new LinkedList()); - private int menuShortcutKeyMask = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); - - private final static int MAX_SERVERS_TO_CLONE = 20; - - public void refreshFrameTitle() { - String s = (String) textArea.getDocument().getProperty("filename"); - if (s == null) - s = "Script" + myScriptNumber; - String title = s.replace('\\','/'); - frame.setTitle(title + (getModified() ? " (not saved) " : "") + (server!=null?" @"+server.toString():"") +" Studio for kdb+ " + Lm.getVersionString()); - } - - public static class WindowListChangedEvent extends EventObject { - public WindowListChangedEvent(Object source) { - super(source); - } - } - - public interface WindowListChangedEventListener extends EventListener { - public void WindowListChangedEventOccurred(WindowListChangedEvent evt); - } - - public static class WindowListMonitor { - protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); - - public synchronized void addEventListener(WindowListChangedEventListener listener) { - listenerList.add(WindowListChangedEventListener.class,listener); - } - - public synchronized void removeEventListener(WindowListChangedEventListener listener) { - listenerList.remove(WindowListChangedEventListener.class,listener); - } - - synchronized void fireMyEvent(WindowListChangedEvent evt) { - Object[] listeners = listenerList.getListenerList(); - for (int i = 0;i < listeners.length;i += 2) - if (listeners[i] == WindowListChangedEventListener.class) - ((WindowListChangedEventListener) listeners[i + 1]).WindowListChangedEventOccurred(evt); - } - } - public static WindowListMonitor windowListMonitor = new WindowListMonitor(); -/* - private void updateKeyBindings(JEditorPane editorPane) { - InputMap inputMap = editorPane.getInputMap(); - inputMap.put(KeyStroke.getKeyStroke("DELETE"),ExtKit.deleteNextCharAction); - inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"),ExtKit.deletePrevCharAction); - inputMap.put(KeyStroke.getKeyStroke("ENTER"),ExtKit.insertBreakAction); - inputMap.put(KeyStroke.getKeyStroke("UP"),ExtKit.upAction); - inputMap.put(KeyStroke.getKeyStroke("DOWN"),ExtKit.downAction); - inputMap.put(KeyStroke.getKeyStroke("LEFT"),ExtKit.backwardAction); - inputMap.put(KeyStroke.getKeyStroke("RIGHT"),ExtKit.forwardAction); - inputMap.put(KeyStroke.getKeyStroke("ctrl Z"),ExtKit.undoAction); - inputMap.put(KeyStroke.getKeyStroke("ctrl Y"),ExtKit.redoAction); - } -*/ - private void updateUndoRedoState(UndoManager um) { - undoAction.setEnabled(um.canUndo()); - redoAction.setEnabled(um.canRedo()); - } - - private void initDocument() { - initActions(); - refreshActionState(); - - Document doc = null; - if (textArea == null) { - textArea = new JEditorPane("text/q",""); - Action[] actions = textArea.getActions(); - - for (int i = 0;i < actions.length;i++) - if (actions[i] instanceof BaseKit.CopyAction) { - copyAction = (BaseKit.CopyAction) actions[i]; - copyAction.putValue(Action.SHORT_DESCRIPTION,"Copy the selected text to the clipboard"); - copyAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "copy.png")); - copyAction.putValue(Action.NAME,I18n.getString("Copy")); - copyAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_C)); - copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_C,menuShortcutKeyMask)); - } - else if (actions[i] instanceof BaseKit.CutAction) { - cutAction = (BaseKit.CutAction) actions[i]; - cutAction.putValue(Action.SHORT_DESCRIPTION,"Cut the selected text"); - cutAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "cut.png")); - cutAction.putValue(Action.NAME,I18n.getString("Cut")); - cutAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_T)); - cutAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_X,menuShortcutKeyMask)); - } - else if (actions[i] instanceof BaseKit.PasteAction) { - pasteAction = (BaseKit.PasteAction) actions[i]; - pasteAction.putValue(Action.SHORT_DESCRIPTION,"Paste text from the clipboard"); - pasteAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "paste.png")); - pasteAction.putValue(Action.NAME,I18n.getString("Paste")); - pasteAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_P)); - pasteAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_V,menuShortcutKeyMask)); - } - else if (actions[i] instanceof ExtKit.FindAction) { - findAction = actions[i]; - findAction.putValue(Action.SHORT_DESCRIPTION,"Find text in the document"); - findAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "find.png")); - findAction.putValue(Action.NAME,I18n.getString("Find")); - findAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_F)); - findAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_F,menuShortcutKeyMask)); - } - else if (actions[i] instanceof ExtKit.ReplaceAction) { - replaceAction = actions[i]; - replaceAction.putValue(Action.SHORT_DESCRIPTION,"Replace text in the document"); - replaceAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "replace.png")); - replaceAction.putValue(Action.NAME,I18n.getString("Replace")); - replaceAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_R)); - replaceAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_R,menuShortcutKeyMask)); - } - else if (actions[i] instanceof BaseKit.SelectAllAction) { - selectAllAction = (BaseKit.SelectAllAction) actions[i]; - selectAllAction.putValue(Action.SHORT_DESCRIPTION,"Select all text in the document"); - selectAllAction.putValue(Action.SMALL_ICON,null); - selectAllAction.putValue(Action.NAME,I18n.getString("SelectAll")); - selectAllAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_A)); - selectAllAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_A,menuShortcutKeyMask)); - } - else if (actions[i] instanceof ActionFactory.UndoAction) { - undoAction = (ActionFactory.UndoAction) actions[i]; - undoAction.putValue(Action.SHORT_DESCRIPTION,"Undo the last change to the document"); - undoAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "undo.png")); - undoAction.putValue(Action.NAME,I18n.getString("Undo")); - undoAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_U)); - undoAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_Z,menuShortcutKeyMask)); - } - else if (actions[i] instanceof ActionFactory.RedoAction) { - redoAction = (ActionFactory.RedoAction) actions[i]; - redoAction.putValue(Action.SHORT_DESCRIPTION,"Redo the last change to the document"); - redoAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "redo.png")); - redoAction.putValue(Action.NAME,I18n.getString("Redo")); - redoAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_R)); - redoAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_Y,menuShortcutKeyMask)); - } - - doc = textArea.getDocument(); - doc.putProperty("filename",null); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - // doc.putProperty("created", Boolean.TRUE); - } - else - doc = textArea.getDocument(); - - JComponent c = (textArea.getUI() instanceof BaseTextUI) ? Utilities.getEditorUI(textArea).getExtComponent() : new JScrollPane(textArea); - - doc.putProperty("server",server); - - MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); - if (mdl == null) { - mdl = new MarkingDocumentListener(c); - doc.putProperty("MarkingDocumentListener",mdl); - doc.addDocumentListener(mdl); - } - mdl.setModified(false); - - UndoManager um = (UndoManager) doc.getProperty(BaseDocument.UNDO_MANAGER_PROP); - if (um == null) { - um = new UndoManager() { - public void undoableEditHappened(UndoableEditEvent e) { - super.undoableEditHappened(e); - updateUndoRedoState(this); - } - - public synchronized void redo() throws CannotRedoException { - super.redo(); - updateUndoRedoState(this); - } - - public synchronized void undo() throws CannotUndoException { - super.undo(); - updateUndoRedoState(this); - } - }; - doc.putProperty(BaseDocument.UNDO_MANAGER_PROP,um); - doc.addUndoableEditListener(um); - } - um.discardAllEdits(); - updateUndoRedoState(um); - - if (splitpane.getTopComponent() != c) { - splitpane.setTopComponent(c); - splitpane.setDividerLocation(0.5); - } - - rebuildToolbar(); - rebuildMenuBar(); - - textArea.requestFocus(); - } - - private void refreshActionState() { - newFileAction.setEnabled(true); - arrangeAllAction.setEnabled(true); - openFileAction.setEnabled(true); - serverListAction.setEnabled(true); - openFileInNewWindowAction.setEnabled(true); - saveFileAction.setEnabled(true); - saveAsFileAction.setEnabled(true); - exportAction.setEnabled(false); - chartAction.setEnabled(false); - openInExcel.setEnabled(false); - stopAction.setEnabled(false); - executeAction.setEnabled(true); - executeCurrentLineAction.setEnabled(true); - refreshAction.setEnabled(false); - -// helpAction.setEnabled(true); - aboutAction.setEnabled(true); - exitAction.setEnabled(true); - settingsAction.setEnabled(true); - } - - private String getFilename() { - JFileChooser chooser = new JFileChooser(); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - - FileFilter ff = - new FileFilter() { - public String getDescription() { - return "q script"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".q")) - return true; - else - return false; - } - }; - - chooser.addChoosableFileFilter(ff); - - chooser.setFileFilter(ff); - - String filename = (String) textArea.getDocument().getProperty("filename"); - if (filename != null) { - File file = new File(filename); - File dir = new File(file.getPath()); - chooser.setCurrentDirectory(dir); - } - - int option = chooser.showOpenDialog(textArea); - - if (option == JFileChooser.APPROVE_OPTION) { - File sf = chooser.getSelectedFile(); - File f = chooser.getCurrentDirectory(); - String dir = f.getAbsolutePath(); - - try { - filename = dir + "/" + sf.getName(); - return filename; - } - catch (Exception e) { - } - } - - return null; - } - - private void exportAsExcel(final String filename) { - new ExcelExporter().exportTableX(frame,table,new File(filename),false); - } - - private void exportAsDelimited(final TableModel model,final String filename,final char delimiter) { - final String message = "Exporting data to " + filename; - - final String note = "0% complete"; - - String title = "Studio for kdb+"; - UIManager.put("ProgressMonitor.progressText",title); - - final int min = 0; - final int max = 100; - final ProgressMonitor pm = new ProgressMonitor(frame,message,note,min,max); - pm.setMillisToDecideToPopup(100); - pm.setMillisToPopup(100); - pm.setProgress(0); - - Runnable runner = new Runnable() { - public void run() { - if (filename != null) { - String lineSeparator = System.getProperty("line.separator");; - - BufferedWriter fw; - - try { - fw = new BufferedWriter(new FileWriter(filename)); - - for (int col = 0;col < model.getColumnCount();col++) { - if (col > 0) - fw.write(delimiter); - - fw.write(model.getColumnName(col)); - } - fw.write(lineSeparator); - - int maxRow = model.getRowCount(); - int lastProgress = 0; - - for (int r = 1;r <= maxRow;r++) { - for (int col = 0;col < model.getColumnCount();col++) { - if (col > 0) - fw.write(delimiter); - - K.KBase o = (K.KBase) model.getValueAt(r - 1,col); - if (!o.isNull()) - fw.write(o.toString(false)); - } - fw.write(lineSeparator); - - boolean cancelled = pm.isCanceled(); - - if (cancelled) - break; - else { - final int progress = (100 * r) / maxRow; - if (progress > lastProgress) { - final String note = "" + progress + "% complete"; - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - pm.setProgress(progress); - pm.setNote(note); - } - }); - - Thread.yield(); - } - } - } - - fw.close(); - } - catch (FileNotFoundException ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - catch (IOException ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - catch (Exception ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - finally { - pm.close(); - } - } - } - }; - - Thread t = new Thread(runner); - t.setName("export"); - t.setPriority(Thread.MIN_PRIORITY); - t.start(); - } - - private void exportAsXml(final TableModel model,final String filename) { - final String message = "Exporting data to " + filename; - - final String note = "0% complete"; - - String title = "Studio for kdb+"; - UIManager.put("ProgressMonitor.progressText",title); - - final int min = 0; - final int max = 100; - final ProgressMonitor pm = new ProgressMonitor(frame,message,note,min,max); - pm.setMillisToDecideToPopup(100); - pm.setMillisToPopup(100); - pm.setProgress(0); - - Runnable runner = new Runnable() { - public void run() { - if (filename != null) { - String lineSeparator = System.getProperty("line.separator");; - - BufferedWriter fw = null; - - try { - fw = new BufferedWriter(new FileWriter(filename)); - - fw.write(""); - - int maxRow = model.getRowCount(); - int lastProgress = 0; - - fw.write(lineSeparator); - - String[] columns = new String[model.getColumnCount()]; - for (int col = 0;col < model.getColumnCount();col++) - columns[col] = model.getColumnName(col); - - for (int r = 1;r <= maxRow;r++) { - fw.write(""); - for (int col = 0;col < columns.length;col++) { - fw.write("<" + columns[col] + ">"); - - K.KBase o = (K.KBase) model.getValueAt(r - 1,col); - if (!o.isNull()) - fw.write(o.toString(false)); - - fw.write(""); - } - fw.write(""); - fw.write(lineSeparator); - - boolean cancelled = pm.isCanceled(); - - if (cancelled) - break; - else { - final int progress = (100 * r) / maxRow; - if (progress > lastProgress) { - final String note = "" + progress + "% complete"; - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - pm.setProgress(progress); - pm.setNote(note); - } - }); - - Thread.yield(); - } - } - } - fw.write(""); - - fw.close(); - } - catch (FileNotFoundException ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - catch (IOException ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - catch (Exception ex) { - ex.printStackTrace(); //To change body of catch statement use Options | File Templates. - } - finally { - pm.close(); - } - } - } - }; - - Thread t = new Thread(runner); - t.setName("export"); - t.setPriority(Thread.MIN_PRIORITY); - t.start(); - } - - private void exportAsTxt(String filename) { - exportAsDelimited(table.getModel(),filename,'\t'); - } - - private void exportAsCSV(String filename) { - exportAsDelimited(table.getModel(),filename,','); - } - - private void export() { - JFileChooser chooser = new JFileChooser(); - chooser.setDialogType(JFileChooser.SAVE_DIALOG); - chooser.setDialogTitle("Export result set as"); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - - FileFilter csvFilter = null; - FileFilter txtFilter = null; - FileFilter xmlFilter = null; - FileFilter xlsFilter = null; - - if (table != null) { - csvFilter = - new FileFilter() { - public String getDescription() { - return "csv (Comma delimited)"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".csv")) - return true; - else - return false; - } - }; - - txtFilter = - new FileFilter() { - public String getDescription() { - return "txt (Tab delimited)"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".txt")) - return true; - else - return false; - } - }; - - xmlFilter = - new FileFilter() { - public String getDescription() { - return "xml"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".xml")) - return true; - else - return false; - } - }; - - - xlsFilter = - new FileFilter() { - public String getDescription() { - return "xls (Microsoft Excel)"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".xls")) - return true; - else - return false; - } - }; - - chooser.addChoosableFileFilter(csvFilter); - chooser.addChoosableFileFilter(txtFilter); - chooser.addChoosableFileFilter(xmlFilter); - chooser.addChoosableFileFilter(xlsFilter); - } - - if (exportFilename != null) { - File file = new File(exportFilename); - File dir = new File(file.getPath()); - chooser.setCurrentDirectory(dir); - chooser.ensureFileIsVisible(file); - if (table != null) - if (exportFilename.endsWith(".xls")) - chooser.setFileFilter(xlsFilter); - else if (exportFilename.endsWith(".csv")) - chooser.setFileFilter(csvFilter); - else if (exportFilename.endsWith(".xml")) - chooser.setFileFilter(xmlFilter); - else if (exportFilename.endsWith(".txt")) - chooser.setFileFilter(txtFilter); - } - - int option = chooser.showSaveDialog(textArea); - - if (option == JFileChooser.APPROVE_OPTION) { - File sf = chooser.getSelectedFile(); - File f = chooser.getCurrentDirectory(); - String dir = f.getAbsolutePath(); - -// Cursor cursor= frame.getCursor(); - - try { - // frame.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)); - FileFilter ff = chooser.getFileFilter(); - - exportFilename = dir + "/" + sf.getName(); - - if (table != null) - if (exportFilename.endsWith(".xls")) - exportAsExcel(exportFilename); - else if (exportFilename.endsWith(".csv")) - exportAsCSV(exportFilename); - else if (exportFilename.endsWith(".txt")) - exportAsTxt(exportFilename); - else if (exportFilename.endsWith(".xml")) - exportAsXml(table.getModel(),exportFilename); - /* else if (exportFilename.endsWith(".res")) { - exportAsBin(exportFilename); - } - */ - else - if (ff == csvFilter) - exportAsCSV(exportFilename); - else if (ff == xlsFilter) - exportAsExcel(exportFilename); - else if (ff == txtFilter) - exportAsTxt(exportFilename); - else if (ff == xmlFilter) - exportAsXml(table.getModel(),exportFilename); - /*else if( ff == binFilter){ - exportAsBin(exportFilename); - } - */ - else - JOptionPane.showMessageDialog(frame, - "Warning", - "You did not specify what format to export the file as.\n Cancelling data export", - JOptionPane.WARNING_MESSAGE, - getImage(Config.imageBase + "32x32/warning.png")); - /* else { - exportAsBin(exportFilename); - } - */ - } - catch (Exception e) { - JOptionPane.showMessageDialog(frame, - "Error", - "An error occurred whilst writing the export file.\n Details are: " + e.getMessage(), - JOptionPane.ERROR_MESSAGE, - getImage(Config.imageBase + "32x32/error.png")); - } - finally { - // frame.setCursor(cursor); - } - } - } - - public void newFile() { - try { - String filename = (String) textArea.getDocument().getProperty("filename"); - if (!saveIfModified(filename)) - return; - - textArea.getDocument().remove(0,textArea.getDocument().getLength()); - textArea.getDocument().putProperty("filename",null); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - initDocument(); - refreshFrameTitle(); - } - catch (BadLocationException ex) { - ex.printStackTrace(); - } - } - - public void openFile() { - String filename = (String) textArea.getDocument().getProperty("filename"); - if (!saveIfModified(filename)) - return; - - filename = getFilename(); - - if (filename != null) { - loadFile(filename); - addToMruFiles(filename); - } - } - // returns true to continue - public boolean saveIfModified(String filename) { - if (getModified()) { - int choice = JOptionPane.showOptionDialog(frame, - "Changes not saved.\nSave now?", - "Save changes?", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, - getImage(Config.imageBase + "32x32/question.png"), - null, // use standard button titles - null); // no default selection - - if (choice == JOptionPane.YES_OPTION) { - try { - if (saveFile(filename,false)) - // was cancelled so return - return false; - } - catch (Exception e) { - return false; - } - return true; - } - else if ((choice == JOptionPane.CANCEL_OPTION) || (choice == JOptionPane.CLOSED_OPTION)) - return false; - } - return true; - } - - public void loadMRUFile(String filename,String oldFilename) { - if (!saveIfModified(oldFilename)) - return; - - loadFile(filename); - addToMruFiles(filename); - setServer(server); - } - - private void addToMruFiles(String filename) { - if (filename == null) - return; - - Vector v = new Vector(); - v.add(filename); - String[] mru = Config.getInstance().getMRUFiles(); - for (int i = 0;i < mru.length;i++) - if (!v.contains(mru[i])) - v.add(mru[i]); - Config.getInstance().saveMRUFiles((String[]) v.toArray(new String[0])); - rebuildMenuBar(); - } - - static public String getContents(File aFile) { - StringBuffer contents = new StringBuffer(); - - try { - InputStreamReader isr = new InputStreamReader(new FileInputStream(aFile), - "UTF-8"); - BufferedReader input = new BufferedReader(isr); - try { - - String line = null; - while ((line = input.readLine()) != null) { - contents.append(line); - contents.append(System.getProperty("line.separator")); - } - } - finally { - input.close(); - } - } - catch (IOException ex) { - ex.printStackTrace(); - } - - return contents.toString(); - } - - public void loadFile(String filename) { - try { - String s = getContents(new File(filename)); - - textArea.getDocument().remove(0,textArea.getDocument().getLength()); - textArea.getDocument().insertString(0,s,null); - textArea.getDocument().putProperty("filename",filename); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - initDocument(); - textArea.setCaretPosition(0); - refreshFrameTitle(); - } - catch (BadLocationException ex) { - ex.printStackTrace(); - } - } - - public boolean saveAsFile() { - JFileChooser chooser = new JFileChooser(); - chooser.setDialogType(JFileChooser.SAVE_DIALOG); - chooser.setDialogTitle("Save script as"); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - - FileFilter ff = - new FileFilter() { - public String getDescription() { - return "q script"; - } - - public boolean accept(File file) { - if (file.isDirectory() || file.getName().endsWith(".q")) - return true; - else - return false; - } - }; - - chooser.addChoosableFileFilter(ff); - - chooser.setFileFilter(ff); - - String filename = (String) textArea.getDocument().getProperty("filename"); - if (filename != null) { - File file = new File(filename); - File dir = new File(file.getPath()); - chooser.setCurrentDirectory(dir); - } - -// chooser.setMultiSelectionEnabled(true); - int option = chooser.showSaveDialog(textArea); - - if (option == JFileChooser.APPROVE_OPTION) { - File sf = chooser.getSelectedFile(); - File f = chooser.getCurrentDirectory(); - String dir = f.getAbsolutePath(); - - try { - filename = dir + "/" + sf.getName(); - sf = new File(filename); - - if (sf.exists()) { - int choice = JOptionPane.showOptionDialog(frame, - filename + " already exists.\nOverwrite?", - "Overwrite?", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, - getImage(Config.imageBase + "32x32/question.png"), - null, // use standard button titles - null); // no default selection - - if (choice != JOptionPane.YES_OPTION) - return false; - } - - return saveFile(filename,true); - } - catch (Exception e) { - } - } - return false; - } - // private boolean wasLoaded=false; - // returns true if saved, false if error or cancelled - public boolean saveFile(String filename,boolean force) { - if (filename == null) - return saveAsFile(); - - try { - if (!force) - if (null == textArea.getDocument().getProperty("filename")) - return saveAsFile(); - - textArea.write(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"))); - textArea.getDocument().putProperty("filename",filename); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - setModified(false); - addToMruFiles(filename); - refreshFrameTitle(); - return true; - } - catch (Exception e) { - } - - return false; - } - - private void arrangeAll() { - int noWins = windowList.size(); - - Iterator i = windowList.iterator(); - - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - - int noRows = noWins > 3 ? 3 : noWins; - int height = screenSize.height / noRows; - - for (int row = 0;row < noRows;row++) { - int noCols = (noWins / 3); - - if ((row == 0) && ((noWins % 3) > 0)) - noCols++; - else if ((row == 1) && ((noWins % 3) > 1)) - noCols++; - - int width = screenSize.width / noCols; - - for (int col = 0;col < noCols;col++) { - Object o = i.next(); - JFrame f; - - if (o instanceof StudioPanel) - f = ((StudioPanel) o).frame; - else - f = (JFrame) o; - - f.setSize(width,height); - f.setLocation(col * width,((noRows - 1) - row) * height); - ensureDeiconified(f); - } - } - } - - private void setModified(boolean value) { - if (textArea != null) { - Document doc = textArea.getDocument(); - - if (doc != null) { - MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); - if (mdl != null) - mdl.setModified(value); - } - } - } - - private boolean getModified() { - if (textArea != null) { - Document doc = textArea.getDocument(); - - if (doc != null) { - MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); - if (mdl != null) - return mdl.getModified(); - } - } - - return true; - } - - private void setServer(Server server) { - if (server == null) - return; - - this.server = server; - - if (textArea != null) { - Document doc = textArea.getDocument(); - - if (doc != null) - doc.putProperty("server",server); - Utilities.getEditorUI(textArea).getComponent().setBackground(server.getBackgroundColor()); - } - - new ReloadQKeywords(server); - Config.getInstance().setLRUServer(server); - - refreshFrameTitle(); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - } - - private void initActions() { - newFileAction = new UserAction(I18n.getString("New"), - getImage(Config.imageBase2 + "document_new.png"), - "Create a blank script", - new Integer(KeyEvent.VK_N), - null) { - public void actionPerformed(ActionEvent e) { - // PrintUtilities.printComponent(textArea); - newFile(); - } - }; - - arrangeAllAction = new UserAction(I18n.getString("ArrangeAll"), - getImage(Config.imageBase2 + "blank.png"), - "Arrange all windows on screen", - new Integer(KeyEvent.VK_A), - null) { - public void actionPerformed(ActionEvent e) { - arrangeAll(); - } - }; - - minMaxDividerAction = new UserAction(I18n.getString("MaximizeEditorPane"), - getImage(Config.imageBase2 + "blank.png"), - "Maximize editor pane", - new Integer(KeyEvent.VK_M), - KeyStroke.getKeyStroke(KeyEvent.VK_M,menuShortcutKeyMask)) { - public void actionPerformed(ActionEvent e) { - minMaxDivider(); - } - }; - - toggleDividerOrientationAction = new UserAction(I18n.getString("ToggleDividerOrientation"), - getImage(Config.imageBase2 + "blank.png"), - "Toggle the window divider's orientation", - new Integer(KeyEvent.VK_C), - null) { - public void actionPerformed(ActionEvent e) { - toggleDividerOrientation(); - } - }; - - closeFileAction = new UserAction(I18n.getString("Close"), - getImage(Config.imageBase2 + "blank.png"), - "Close current document", - new Integer(KeyEvent.VK_C), - null) { - public void actionPerformed(ActionEvent e) { - quitWindow(); - if (windowList.size() == 0) - System.exit(0); - } - }; - - openFileAction = new UserAction(I18n.getString("Open"), - getImage(Config.imageBase2 + "folder.png"), - "Open a script", - new Integer(KeyEvent.VK_O), - KeyStroke.getKeyStroke(KeyEvent.VK_O,menuShortcutKeyMask)) { - public void actionPerformed(ActionEvent e) { - openFile(); - } - }; - - openFileInNewWindowAction = new UserAction(I18n.getString("NewWindow"), - getImage(Config.imageBase2 + "blank.png"), - "Open a new window", - new Integer(KeyEvent.VK_N), - KeyStroke.getKeyStroke(KeyEvent.VK_N, menuShortcutKeyMask) ) { - public void actionPerformed(ActionEvent e) { - new StudioPanel(server,null); - } - }; - - serverListAction = new UserAction(I18n.getString("ServerList"), - getImage(Config.imageBase + "text_tree.png"), - "Show sever list", - new Integer(KeyEvent.VK_L), - KeyStroke.getKeyStroke(KeyEvent.VK_L, menuShortcutKeyMask | Event.SHIFT_MASK) ) { - public void actionPerformed(ActionEvent e) { - ServerList serverList = new ServerList(frame, Config.getInstance().getServers(), server); - serverList.alignAndShow(); - Server selectedServer = serverList.getSelectedServer(); - if (selectedServer.equals(server)) return; - - setServer(selectedServer); - rebuildToolbar(); - } - }; - - editServerAction = new UserAction(I18n.getString("Edit"), - getImage(Config.imageBase2 + "server_information.png"), - "Edit the server details", - new Integer(KeyEvent.VK_E), - null) { - public void actionPerformed(ActionEvent e) { - Server s = new Server(server); - - EditServerForm f = new EditServerForm(frame,s); - f.alignAndShow(); - if (f.getResult() == ACCEPTED) { - if (stopAction.isEnabled()) - stopAction.actionPerformed(e); - - ConnectionPool.getInstance().purge(server); - Config.getInstance().removeServer(server); - - s = f.getServer(); - Config.getInstance().addServer(s); - setServer(s); - rebuildToolbar(); - rebuildMenuBar(); - - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - } - } - }; - - - addServerAction = new UserAction(I18n.getString("Add"), - getImage(Config.imageBase2 + "server_add.png"), - "Configure a new server", - new Integer(KeyEvent.VK_A), - null) { - public void actionPerformed(ActionEvent e) { - AddServerForm f = new AddServerForm(frame); - f.alignAndShow(); - if (f.getResult() == ACCEPTED) { - Server s = f.getServer(); - Config.getInstance().addServer(s); - ConnectionPool.getInstance().purge(s); //? - setServer(s); - rebuildToolbar(); - rebuildMenuBar(); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - } - } - }; - - removeServerAction = new UserAction(I18n.getString("Remove"), - getImage(Config.imageBase2 + "server_delete.png"), - "Remove this server", - new Integer(KeyEvent.VK_R), - null) { - public void actionPerformed(ActionEvent e) { - int choice = JOptionPane.showOptionDialog(frame, - "Remove server " + server.getName() + " from list?", - "Remove server?", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, - getImage(Config.imageBase + "32x32/question.png"), - null, // use standard button titles - null); // no default selection - - if (choice == 0) { - Config.getInstance().removeServer(server); - - Server[] servers = Config.getInstance().getServers(); - - if (servers.length > 0) - setServer(servers[0]); - - rebuildToolbar(); - rebuildMenuBar(); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - } - } - }; - - - saveFileAction = new UserAction(I18n.getString("Save"), - getImage(Config.imageBase2 + "disks.png"), - "Save the script", - new Integer(KeyEvent.VK_S), - KeyStroke.getKeyStroke(KeyEvent.VK_S,menuShortcutKeyMask)) { - public void actionPerformed(ActionEvent e) { - String filename = (String) textArea.getDocument().getProperty("filename"); - saveFile(filename,false); - } - }; - - saveAsFileAction = new UserAction(I18n.getString("SaveAs"), - getImage(Config.imageBase2 + "save_as.png"), - "Save script as", - new Integer(KeyEvent.VK_A), - null) { - public void actionPerformed(ActionEvent e) { - saveAsFile(); - } - }; - - exportAction = new UserAction(I18n.getString("Export"), - getImage(Config.imageBase2 + "export2.png"), - "Export result set", - new Integer(KeyEvent.VK_E), - null) { - public void actionPerformed(ActionEvent e) { - export(); - } - }; - - chartAction = new UserAction(I18n.getString("Chart"), - Util.getImage(Config.imageBase2 + "chart.png"), - "Chart current data set", - new Integer(KeyEvent.VK_E), - null) { - public void actionPerformed(ActionEvent e) { - new LineChart((KTableModel) table.getModel()); - //new PriceVolumeChart(table); - } - }; - - - stopAction = new UserAction(I18n.getString("Stop"), - getImage(Config.imageBase2 + "stop.png"), - "Stop the query", - new Integer(KeyEvent.VK_S), - null) { - public void actionPerformed(ActionEvent e) { - if (worker != null) { - worker.interrupt(); - stopAction.setEnabled(false); - textArea.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); - } - } - }; - - - openInExcel = new UserAction(I18n.getString("OpenInExcel"), - getImage(Config.imageBase + "excel_icon.gif"), - "Open in Excel", - new Integer(KeyEvent.VK_O), - null) { - - public void actionPerformed(ActionEvent e) { - try { - File file = File.createTempFile("studioExport",".xls"); - new ExcelExporter().exportTableX(frame,table,file,true); - } - catch (IOException ex) { - ex.printStackTrace(); - } - } - }; - - - executeAction = new UserAction(I18n.getString("Execute"), - Util.getImage(Config.imageBase2 + "table_sql_run.png"), - "Execute the full or highlighted text as a query", - new Integer(KeyEvent.VK_E), - KeyStroke.getKeyStroke(KeyEvent.VK_E,menuShortcutKeyMask)) { - - public void actionPerformed(ActionEvent e) { - executeQuery(); - } - }; - - - executeCurrentLineAction = new UserAction(I18n.getString("ExecuteCurrentLine"), - Util.getImage(Config.imageBase2 + "element_run.png"), - "Execute the current line as a query", - new Integer(KeyEvent.VK_ENTER), - KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,menuShortcutKeyMask)) { - - public void actionPerformed(ActionEvent e) { - executeQueryCurrentLine(); - } - }; - - - refreshAction = new UserAction(I18n.getString("Refresh"), - getImage(Config.imageBase2 + "refresh.png"), - "Refresh the result set", - new Integer(KeyEvent.VK_R), - KeyStroke.getKeyStroke(KeyEvent.VK_Y,menuShortcutKeyMask | Event.SHIFT_MASK)) { - - public void actionPerformed(ActionEvent e) { - refreshQuery(); - } - }; - - aboutAction = new UserAction(I18n.getString("About"), - Util.getImage(Config.imageBase2 + "about.png"), - "About Studio for kdb+", - new Integer(KeyEvent.VK_E), - null) { - - public void actionPerformed(ActionEvent e) { - about(); - } - }; - - exitAction = new UserAction(I18n.getString("Exit"), - getImage(Config.imageBase2 + "blank.png"), - "Close this window", - new Integer(KeyEvent.VK_X), - null) { - - public void actionPerformed(ActionEvent e) { - if (quit()) - System.exit(0); - } - }; - - settingsAction = new UserAction("Settings", - getImage(Config.imageBase2 + "blank.png"), - "Settings", - new Integer(KeyEvent.VK_S), - null) { - - public void actionPerformed(ActionEvent e) { - settings(); - } - }; - - codeKxComAction = new UserAction("code.kx.com", - Util.getImage(Config.imageBase2 + "text.png"), - "Open code.kx.com", - new Integer(KeyEvent.VK_C), - null) { - - public void actionPerformed(ActionEvent e) { - try { - BrowserLaunch.openURL("http://code.kx.com/trac/wiki/Reference"); - } catch (Exception ex) { - JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + ex.getLocalizedMessage()); - } - } - }; - } - - public void settings() { - SettingsDialog dialog = new SettingsDialog(frame); - dialog.alignAndShow(); - if (dialog.getResult() == CANCELLED) return; - - String auth = dialog.getDefaultAuthenticationMechanism(); - Config.getInstance().setDefaultAuthMechanism(auth); - Config.getInstance().setDefaultCredentials(auth, new Credentials(dialog.getUser(), dialog.getPassword())); - } - - public void about() { - HelpDialog help = new HelpDialog(frame); - Util.centerChildOnParent(help,frame); - // help.setTitle("About Studio for kdb+"); - help.pack(); - help.setVisible(true); - } - - public boolean quit() { - boolean okToExit = true; - - Object[] objs = windowList.toArray(); - - for (int i = 0;i < objs.length;i++) { - Object o = objs[i]; - - if (o instanceof StudioPanel) { - if (!((StudioPanel) o).quitWindow()) - okToExit = false; - } - else - if (o instanceof JFrame) { - JFrame f = (JFrame) o; - f.setVisible(false); - - f.dispose(); - } - } - - return okToExit; - } - - public boolean quitWindow() { - if (getModified()) { - int choice = JOptionPane.showOptionDialog(frame, - "Changes not saved.\nSave now?", - "Save changes?", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, - getImage(Config.imageBase + "32x32/question.png"), - null, // use standard button titles - null); // no default selection - - if (choice == 0) - try { - String filename = (String) textArea.getDocument().getProperty("filename"); - if (!saveFile(filename,false)) - // was cancelled so return - return false; - } - catch (Exception e) { - return false; - } - else if ((choice == 2) || (choice == JOptionPane.CLOSED_OPTION)) - return false; - } - - windowList.remove(this); - windowListMonitor.removeEventListener(windowListChangedEventListener); - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - frame.dispose(); - - return true; - } - - private void rebuildMenuBar() { - menubar = createMenuBar(); - SwingUtilities.invokeLater( - new Runnable() { - - public void run() { - if (frame != null) { - frame.setJMenuBar(menubar); - menubar.validate(); - menubar.repaint(); - frame.validate(); - frame.repaint(); - } - } - }); - } - - private JMenuBar createMenuBar() { - JMenuBar menubar = new JMenuBar(); - JMenu menu = new JMenu(I18n.getString("File")); - menu.setMnemonic(KeyEvent.VK_F); - menu.add(new JMenuItem(newFileAction)); - menu.add(new JMenuItem(openFileAction)); - menu.add(new JMenuItem(saveFileAction)); - menu.add(new JMenuItem(saveAsFileAction)); - - menu.add(new JMenuItem(closeFileAction)); - - if (!MAC_OS_X) { - menu.add(new JMenuItem(settingsAction)); - } - menu.addSeparator(); -// menu.add(new JMenuItem(importAction)); - menu.add(new JMenuItem(openInExcel)); - menu.addSeparator(); - menu.add(new JMenuItem(exportAction)); - menu.addSeparator(); - menu.add(new JMenuItem(chartAction)); - - String[] mru = Config.getInstance().getMRUFiles(); - - if (mru.length > 0) { - menu.addSeparator(); - char[] mnems = "123456789".toCharArray(); - - for (int i = 0;i < (mru.length > mnems.length ? mnems.length : mru.length);i++) { - final String filename = mru[i]; - - JMenuItem item = new JMenuItem("" + (i + 1) + " " + filename); - item.setMnemonic(mnems[i]); - item.setIcon(getImage(Config.imageBase2 + "blank.png")); - item.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - loadMRUFile(filename,(String) textArea.getDocument().getProperty("filename")); - } - }); - menu.add(item); - } - } - - if (!MAC_OS_X) { - menu.addSeparator(); - menu.add(new JMenuItem(exitAction)); - } - menubar.add(menu); - - menu = new JMenu(I18n.getString("Edit")); - menu.setMnemonic(KeyEvent.VK_E); - menu.add(new JMenuItem(undoAction)); - menu.add(new JMenuItem(redoAction)); - menu.addSeparator(); - menu.add(new JMenuItem(cutAction)); - menu.add(new JMenuItem(copyAction)); - menu.add(new JMenuItem(pasteAction)); - menu.addSeparator(); - menu.add(new JMenuItem(selectAllAction)); - menu.addSeparator(); - menu.add(new JMenuItem(findAction)); - menu.add(new JMenuItem(replaceAction)); -// menu.addSeparator(); -// menu.add(new JMenuItem(editFontAction)); - menubar.add(menu); - - menu = new JMenu(I18n.getString("Server")); - menu.setMnemonic(KeyEvent.VK_S); - menu.add(new JMenuItem(addServerAction)); - menu.add(new JMenuItem(editServerAction)); - menu.add(new JMenuItem(removeServerAction)); - - Server[] servers = Config.getInstance().getServers(); - if (servers.length > 0) { - JMenu subMenu = new JMenu(I18n.getString("Clone")); - subMenu.setIcon(Util.getImage(Config.imageBase2 + "data_copy.png")); - - int count = MAX_SERVERS_TO_CLONE; - for (int i = 0;i < servers.length;i++) { - final Server s = servers[i]; - if (!s.equals(server) && count <= 0) continue; - count--; - JMenuItem item = new JMenuItem(s.getName()); - item.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - Server clone = new Server(s); - clone.setName("Clone of " + clone.getName()); - - EditServerForm f = new EditServerForm(frame,clone); - f.alignAndShow(); - - if (f.getResult() == ACCEPTED) { - clone = f.getServer(); - Config.getInstance().addServer(clone); - //ebuildToolbar(); - setServer(clone); - ConnectionPool.getInstance().purge(clone); //? - windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); - } - } - }); - - subMenu.add(item); - } - - menu.add(subMenu); - } - - menubar.add(menu); - - menu = new JMenu(I18n.getString("Query")); - menu.setMnemonic(KeyEvent.VK_Q); - menu.add(new JMenuItem(executeCurrentLineAction)); - menu.add(new JMenuItem(executeAction)); - menu.add(new JMenuItem(stopAction)); - menu.add(new JMenuItem(refreshAction)); - menubar.add(menu); - - menu = new JMenu(I18n.getString("Window")); - menu.setMnemonic(KeyEvent.VK_W); - - menu.add(new JMenuItem(minMaxDividerAction)); - menu.add(new JMenuItem(toggleDividerOrientationAction)); - menu.add(new JMenuItem(openFileInNewWindowAction)); - menu.add(new JMenuItem(arrangeAllAction)); - menu.add(new JMenuItem(serverListAction)); - - if (windowList.size() > 0) { - menu.addSeparator(); - - int i = 0; - Iterator it = windowList.iterator(); - - while (it.hasNext()) { - String t = "unknown"; - - final Object o = it.next(); - - if (o instanceof StudioPanel) { - StudioPanel r = (StudioPanel) o; - String filename = (String) r.textArea.getDocument().getProperty("filename"); - - if (filename != null) - t = filename.replace('\\','/'); - - if (r.server != null) - t = t + "[" + r.server.getName() + "]"; - else - t = t + "[no server]"; - } - else - if (o instanceof JFrame) - t = ((JFrame) o).getTitle(); - - JMenuItem item = new JMenuItem("" + (i + 1) + " " + t); - item.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - if (o instanceof StudioPanel) { - JFrame f = ((StudioPanel) o).frame; - ensureDeiconified(f); - } - else - ensureDeiconified((JFrame) o); - } - }); - - if (o == this) - item.setIcon(getImage(Config.imageBase2 + "check2.png")); - else - item.setIcon(getImage(Config.imageBase2 + "blank.png")); - - menu.add(item); - i++; - } - } - menubar.add(menu); - menu = new JMenu(I18n.getString("Help")); - menu.setMnemonic(KeyEvent.VK_H); - menu.add(new JMenuItem(codeKxComAction)); - if (!MAC_OS_X) - menu.add(new JMenuItem(aboutAction)); - menubar.add(menu); - - return menubar; - } - - private void ensureDeiconified(JFrame f) { - int state = f.getExtendedState(); - state = state & ~Frame.ICONIFIED; - f.setExtendedState(state); - f.show(); - } - - private void selectConnectionString() { - String connection = txtServer.getText().trim(); - if (connection.length() == 0) return; - if (server != null && server.getConnectionString(false).equals(connection)) return; - - try { - setServer(Config.getInstance().getServerByConnectionString(connection)); - - rebuildToolbar(); - toolbar.validate(); - toolbar.repaint(); - } catch (IllegalArgumentException e) { - refreshConnection(); - } - } - - private void selectServerName() { - String selection = comboServer.getSelectedItem().toString(); - if(! Config.getInstance().getServerNames().contains(selection)) return; - - setServer(Config.getInstance().getServer(selection)); - rebuildToolbar(); - toolbar.validate(); - toolbar.repaint(); - } - - private void refreshConnection() { - if (server == null) { - txtServer.setText(""); - txtServer.setToolTipText("Select connection details"); - } else { - txtServer.setText(server.getConnectionString(false)); - txtServer.setToolTipText(server.getConnectionString(true)); - } - } - - private void toolbarAddServerSelection() { - List names = Config.getInstance().getServerNames(); - String name = server == null ? "" : server.getName(); - if (!names.contains(name)) { - List newNames = new ArrayList<>(); - newNames.add(name); - newNames.addAll(names); - names = newNames; - } - comboServer = new JComboBox<>(names.toArray(new String[0])); - comboServer.setToolTipText("Select the server context"); - comboServer.setSelectedItem(name); - comboServer.addActionListener(e->selectServerName()); - - txtServer = new JTextField(); - txtServer.addActionListener(e -> selectConnectionString()); - txtServer.addFocusListener(new FocusAdapter() { - @Override - public void focusLost(FocusEvent e) { - selectConnectionString(); - } - }); - refreshConnection(); - - toolbar.add(new JLabel(I18n.getString("Server"))); - toolbar.add(comboServer); - toolbar.add(txtServer); - toolbar.add(serverListAction); - toolbar.addSeparator(); - } - - private void rebuildToolbar() { - if (toolbar != null) { - toolbar.removeAll(); - toolbarAddServerSelection(); - if (server == null) { - addServerAction.setEnabled(true); - editServerAction.setEnabled(false); - removeServerAction.setEnabled(false); - stopAction.setEnabled(false); - executeAction.setEnabled(false); - executeCurrentLineAction.setEnabled(false); - refreshAction.setEnabled(false); - } - else { - executeAction.setEnabled(true); - executeCurrentLineAction.setEnabled(true); - editServerAction.setEnabled(true); - removeServerAction.setEnabled(true); - } - - toolbar.add(stopAction); - toolbar.add(executeAction); - toolbar.add(refreshAction); - toolbar.addSeparator(); - - toolbar.add(openFileAction); - toolbar.add(saveFileAction); - toolbar.add(saveAsFileAction); - toolbar.addSeparator(); -// toolbar.add(importAction); - toolbar.add(openInExcel); - toolbar.addSeparator(); - toolbar.add(exportAction); - toolbar.addSeparator(); - - toolbar.add(chartAction); - toolbar.addSeparator(); - - toolbar.add(undoAction); - toolbar.add(redoAction); - toolbar.addSeparator(); - - toolbar.add(cutAction); - toolbar.add(copyAction); - toolbar.add(pasteAction); - - toolbar.addSeparator(); - toolbar.add(findAction); - - toolbar.add(replaceAction); - - toolbar.addSeparator(); - toolbar.add(codeKxComAction); - - for (int j = 0;j < toolbar.getComponentCount();j++) { - Component c = toolbar.getComponentAtIndex(j); - - if (c instanceof JButton) - ((JButton) c).setRequestFocusEnabled(false); - } - } - } - - private JToolBar createToolbar() { - toolbar = new JToolBar(); - toolbar.setFloatable(false); - rebuildToolbar(); - return toolbar; - } - - private static class Impl extends FileView implements - LocaleSupport.Localizer { - // FileView implementation - - public String getName(File f) { - return null; - } - - - public String getDescription(File f) { - return null; - } - - - public String getTypeDescription(File f) { - return null; - } - - - public Boolean isTraversable(File f) { - return null; - } - - - public Icon getIcon(File f) { - if (f.isDirectory()) - return null; - // KitInfo ki = KitInfo.getKitInfoForFile(f); - // return ki == null ? null : ki.getIcon(); - return null; - } - private ResourceBundle bundle; - - public Impl(String bundleName) { - bundle = ResourceBundle.getBundle(bundleName); - } - // Localizer - - public String getString(String key) { - return bundle.getString(key); - } - } - private WindowListChangedEventListener windowListChangedEventListener; - - private int dividerLastPosition; // updated from property change listener - private void minMaxDivider(){ - //BasicSplitPaneDivider divider = ((BasicSplitPaneUI)splitpane.getUI()).getDivider(); - //((JButton)divider.getComponent(0)).doClick(); - //((JButton)divider.getComponent(1)).doClick(); - if(splitpane.getDividerLocation()>=splitpane.getMaximumDividerLocation()){ - // Minimize editor pane - splitpane.getTopComponent().setMinimumSize(new Dimension()); - splitpane.getBottomComponent().setMinimumSize(null); - splitpane.setDividerLocation(0.); - splitpane.setResizeWeight(0.); - } - else if(splitpane.getDividerLocation()<=splitpane.getMinimumDividerLocation()){ - // Restore editor pane - splitpane.getTopComponent().setMinimumSize(null); - splitpane.getBottomComponent().setMinimumSize(null); - splitpane.setResizeWeight(0.); - // Could probably catch resize edge-cases etc in pce too - if(dividerLastPosition>=splitpane.getMaximumDividerLocation()||dividerLastPosition<=splitpane.getMinimumDividerLocation()) - dividerLastPosition=splitpane.getMaximumDividerLocation()/2; - splitpane.setDividerLocation(dividerLastPosition); - } - else{ - // Maximize editor pane - splitpane.getBottomComponent().setMinimumSize(new Dimension()); - splitpane.getTopComponent().setMinimumSize(null); - splitpane.setDividerLocation(splitpane.getOrientation()==VERTICAL_SPLIT?splitpane.getHeight()-splitpane.getDividerSize():splitpane.getWidth()-splitpane.getDividerSize()); - splitpane.setResizeWeight(1.); - } - } - - private void toggleDividerOrientation() { - if (splitpane.getOrientation() == JSplitPane.VERTICAL_SPLIT) - splitpane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); - else - splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT); - - splitpane.setDividerLocation(0.5); - } - - public StudioPanel(Server server,String filename) { - - registerForMacOSXEvents(); - - windowListChangedEventListener = new WindowListChangedEventListener() { - - public void WindowListChangedEventOccurred(WindowListChangedEvent evt) { - rebuildMenuBar(); - rebuildToolbar(); - } - }; - - windowListMonitor.addEventListener(windowListChangedEventListener); - - splitpane = new JSplitPane(); - frame = new JFrame(); - windowList.add(this); - - initDocument(); - setServer(server); - - menubar = createMenuBar(); - toolbar = createToolbar(); - - tabbedPane = new JTabbedPane(); - splitpane.setBottomComponent(tabbedPane); - splitpane.setOneTouchExpandable(true); - splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT); - try { - Component divider = ((BasicSplitPaneUI) splitpane.getUI()).getDivider(); - - divider.addMouseListener(new MouseAdapter() { - - public void mouseClicked(MouseEvent event) { - if (event.getClickCount() == 2) - toggleDividerOrientation(); - } - }); - } - catch (ClassCastException e) { - } - splitpane.setContinuousLayout(true); - - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - - frame.setJMenuBar(menubar); - - if (filename != null) - loadFile(filename); - else - myScriptNumber = scriptNumber++; - - refreshFrameTitle(); - - frame.getContentPane().add(toolbar,BorderLayout.NORTH); - frame.getContentPane().add(splitpane,BorderLayout.CENTER); - // frame.setSize(frame.getContentPane().getPreferredSize()); - - frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); - frame.addWindowListener(this); - frame.setSize((int) (0.8 * screenSize.width), - (int) (0.8 * screenSize.height)); - - frame.setLocation(((int) Math.max(0,(screenSize.width - frame.getWidth()) / 2.0)), - (int) (Math.max(0,(screenSize.height - frame.getHeight()) / 2.0))); - - frame.setIconImage(getImage(Config.imageBase + "32x32/dot-chart.png").getImage()); - - // frame.pack(); - frame.setVisible(true); - splitpane.setDividerLocation(0.5); - - textArea.requestFocus(); - splitpane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY,new PropertyChangeListener(){ - public void propertyChange(PropertyChangeEvent pce){ - String s=splitpane.getDividerLocation()>=splitpane.getMaximumDividerLocation()?I18n.getString("MinimizeEditorPane"):splitpane.getDividerLocation()<=splitpane.getMinimumDividerLocation()?I18n.getString("RestoreEditorPane"):I18n.getString("MaximizeEditorPane"); - minMaxDividerAction.putValue(Action.SHORT_DESCRIPTION,s); - minMaxDividerAction.putValue(Action.NAME,s); - if(splitpane.getDividerLocation()splitpane.getMinimumDividerLocation()) - dividerLastPosition=splitpane.getDividerLocation(); - } - }); - dividerLastPosition=splitpane.getDividerLocation(); - } - - public void update(Observable obs,Object obj) { - } - public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x")); - private static boolean registeredForMaxOSXEvents = false; - - public void registerForMacOSXEvents() { - if (registeredForMaxOSXEvents) - return; - - if (MAC_OS_X) - try { - // Generate and register the OSXAdapter, passing it a hash of all the methods we wish to - // use as delegates for various com.apple.eawt.ApplicationListener methods - OSXAdapter.setQuitHandler(this, StudioPanel.class.getDeclaredMethod("quit",(Class[]) null)); - OSXAdapter.setAboutHandler(this, StudioPanel.class.getDeclaredMethod("about",(Class[]) null)); - OSXAdapter.setPreferencesHandler(this, StudioPanel.class.getDeclaredMethod("settings",(Class[]) null)); - registeredForMaxOSXEvents = true; - } - catch (Exception e) { - System.err.println("Error while loading the OSXAdapter:"); - e.printStackTrace(); - } - } - - public static void init(String[] args) { - try { - String filename = null; - - String[] mruFiles = Config.getInstance().getMRUFiles(); - if(args.length>0){ - File f=new File(args[0]); - if(f.exists()) - filename=args[0]; - } else if (mruFiles.length > 0) { - File f = new File(mruFiles[0]); - if (f.exists()) - filename = mruFiles[0]; - } - - Locale.setDefault(Locale.US); - - Server s = null; - String lruServer = Config.getInstance().getLRUServer(); - if (Config.getInstance().getServerNames().contains(lruServer)){ - s = Config.getInstance().getServer(lruServer); - } - new StudioPanel(s,filename); - } - catch (Exception e) { - e.printStackTrace(); - } - } - - public void refreshQuery() { - table = null; - executeK4Query(lastQuery); - } - - public void executeQueryCurrentLine() { - executeQuery(getCurrentLineEditorText(textArea)); - } - - public void executeQuery() { - executeQuery(getEditorText(textArea)); - } - - private void executeQuery(String text) { - table = null; - - if (text == null) { - JOptionPane.showMessageDialog(frame, - "\nNo text available to submit to server.\n\n", - "Studio for kdb+", - JOptionPane.OK_OPTION, - getImage(Config.imageBase + "32x32/information.png")); - - return; - } - - refreshAction.setEnabled(false); - stopAction.setEnabled(true); - executeAction.setEnabled(false); - executeCurrentLineAction.setEnabled(false); - exportAction.setEnabled(false); - chartAction.setEnabled(false); - openInExcel.setEnabled(false); - - executeK4Query(text); - - lastQuery = text; - } - - private String getEditorText(JEditorPane editor) { - String text = editor.getSelectedText(); - - if (text != null) { - if (text.length() > 0) - if (text.trim().length() == 0) - return null; // selected text is whitespace - } - else - text = editor.getText(); // get the full text then - - if (text != null) - text = text.trim(); - - if (text.trim().length() == 0) - text = null; - - return text; - } - - private String getCurrentLineEditorText(JEditorPane editor) { - String newLine = "\n"; - String text = null; - - try { - int pos = editor.getCaretPosition(); - int max = editor.getDocument().getLength(); - - - if ((max > pos) && (!editor.getText(pos,1).equals("\n"))) { - String toeol = editor.getText(pos,max - pos); - int eol = toeol.indexOf('\n'); - - if (eol > 0) - pos = pos + eol; - else - pos = max; - } - - text = editor.getText(0,pos); - - int lrPos = text.lastIndexOf(newLine); - - if (lrPos >= 0) { - lrPos += newLine.length(); // found it so skip it - text = text.substring(lrPos,pos).trim(); - } - } - catch (BadLocationException e) { - } - - if (text != null) { - text = text.trim(); - - if (text.length() == 0) - text = null; - } - - return text; - } - - private void processK4Results(K.KBase r) throws c.K4Exception { - if (r != null) { - exportAction.setEnabled(true); - KTableModel model = KTableModel.getModel(r); - if (model != null) { - boolean dictModel = model instanceof DictModel; - boolean listModel = model instanceof ListModel; - boolean tableModel = ! (dictModel || listModel); - QGrid grid = new QGrid(model); - table = grid.getTable(); - openInExcel.setEnabled(true); - chartAction.setEnabled(tableModel); - String title = tableModel ? "Table" : (dictModel ? "Dict" : "List"); - TabPanel frame = new TabPanel( title + " [" + grid.getRowCount() + " rows] ", - getImage(Config.imageBase2 + "table.png"), - grid); -// frame.setTitle(I18n.getString("Table")+" [" + grid.getRowCount() + " "+I18n.getString("rows")+"] "); - tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); - } else { - chartAction.setEnabled(false); - openInExcel.setEnabled(false); - LimitedWriter lm = new LimitedWriter(50000); - try { - if(!(r instanceof K.UnaryPrimitive&&0==((K.UnaryPrimitive)r).getPrimitiveAsInt())) - r.toString(lm,true); - } - catch (IOException ex) { - ex.printStackTrace(); - } - catch (LimitedWriter.LimitException ex) { - } - - JEditorPane pane = new JEditorPane("text/plain",lm.toString()); - pane.setFont(font); - -//pane.setLineWrap( false); -//pane.setWrapStyleWord( false); - - JScrollPane scrollpane = new JScrollPane(pane, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); - - TabPanel frame = new TabPanel("Console View ", - getImage(Config.imageBase2 + "console.png"), - scrollpane); - - frame.setTitle(I18n.getString("ConsoleView")); - - tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); - } - } - else { - // Log that execute was successful - } - } - Server server = null; - - public void executeK4Query(final String text) { - final Cursor cursor = textArea.getCursor(); - - textArea.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)); - tabbedPane.removeAll(); - worker = new SwingWorker() { - Server s = null; - c c = null; - K.KBase r = null; - Throwable exception; - boolean cancelled = false; - long execTime=0; - public void interrupt() { - super.interrupt(); - - cancelled = true; - - if (c != null) - c.close(); - cleanup(); - } - - public Object construct() { - try { - this.s = server; - c = ConnectionPool.getInstance().leaseConnection(s); - c.setFrame(frame); - long startTime=System.currentTimeMillis(); - c.k(new K.KCharacterVector(text)); - r = c.getResponse(); - execTime=System.currentTimeMillis()-startTime; - } - catch (Throwable e) { - System.err.println("Error occurred during query execution: " + e); - e.printStackTrace(System.err); - exception = e; - } - - return null; - } - - public void finished() { - if (!cancelled) { - if (exception != null) - try { - throw exception; - } - catch (IOException ex) { - JOptionPane.showMessageDialog(frame, - "\nA communications error occurred whilst sending the query.\n\nPlease check that the server is running on " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + ex.getMessage() + "\n\n", - "Studio for kdb+", - JOptionPane.ERROR_MESSAGE, - getImage(Config.imageBase + "32x32/error.png")); - } - catch (c.K4Exception ex) { - JTextPane pane = new JTextPane(); - String hint = QErrors.lookup(ex.getMessage()); - if (hint != null) - hint = "\nStudio Hint: Possibly this error refers to " + hint; - else - hint = ""; - pane.setText("An error occurred during execution of the query.\nThe server sent the response:\n" + ex.getMessage() + hint); - pane.setForeground(Color.RED); - - JScrollPane scrollpane = new JScrollPane(pane); - - TabPanel frame = new TabPanel("Error Details ", - getImage(Config.imageBase2 + "error.png"), - scrollpane); - frame.setTitle("Error Details "); - - tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); - - // tabbedPane.setSelectedComponent(resultsTabbedPane); - } - catch (java.lang.OutOfMemoryError ex) { - JOptionPane.showMessageDialog(frame, - "\nOut of memory whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nThe result set is probably too large.\n\nTry increasing the memory available to studio through the command line option -J -Xmx512m\n\n", - "Studio for kdb+", - JOptionPane.ERROR_MESSAGE, - getImage(Config.imageBase + "32x32/error.png")); - } - catch (Throwable ex) { - String message = ex.getMessage(); - - if ((message == null) || (message.length() == 0)) - message = "No message with exception. Exception is " + ex.toString(); - - JOptionPane.showMessageDialog(frame, - "\nAn unexpected error occurred whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + message + "\n\n", - "Studio for kdb+", - JOptionPane.ERROR_MESSAGE, - getImage(Config.imageBase + "32x32/error.png")); - } - else - try { - Utilities.setStatusText(textArea, "Last execution time:"+(execTime>0?""+execTime:"<1")+" mS"); - processK4Results(r); - } - catch (Exception e) { - JOptionPane.showMessageDialog(frame, - "\nAn unexpected error occurred whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + e.getMessage() + "\n\n", - "Studio for kdb+", - JOptionPane.ERROR_MESSAGE, - getImage(Config.imageBase + "32x32/error.png")); - } - - cleanup(); - } - } - - private void cleanup() { - if (c != null) - ConnectionPool.getInstance().freeConnection(s,c); - //if( c != null) - // c.close(); - c = null; - - textArea.setCursor(cursor); - - stopAction.setEnabled(false); - executeAction.setEnabled(true); - executeCurrentLineAction.setEnabled(true); - refreshAction.setEnabled(true); - - System.gc(); - - worker = null; - } - }; - - worker.start(); - } - private SwingWorker worker; - - public void windowClosing(WindowEvent e) { - if (quitWindow()) - if (windowList.size() == 0) - System.exit(0); - } - - - public void windowClosed(WindowEvent e) { - } - - - public void windowOpened(WindowEvent e) { - } - // ctrl-alt spacebar to minimize window - - public void windowIconified(WindowEvent e) { - } - - - public void windowDeiconified(WindowEvent e) { - } - - - public void windowActivated(WindowEvent e) { - this.invalidate(); - SwingUtilities.updateComponentTreeUI(this); - } - - - public void windowDeactivated(WindowEvent e) { - } - - private class MarkingDocumentListener implements DocumentListener { - private boolean modified = false; - - private void setModified(boolean b) { - modified = b; - } - - private boolean getModified() { - return modified; - } - private Component comp; - - public MarkingDocumentListener(Component comp) { - this.comp = comp; - } - - private void markChanged(DocumentEvent evt) { - setModified(true); - refreshFrameTitle(); - } - - - public void changedUpdate(DocumentEvent e) { - } - - - public void insertUpdate(DocumentEvent evt) { - markChanged(evt); - } - - - public void removeUpdate(DocumentEvent evt) { - markChanged(evt); - } - /** Document property holding String name of associated file */ - private static final String FILE = "file"; - /** Document property holding Boolean if document was created or opened */ - private static final String CREATED = "created"; - /** Document property holding Boolean modified information */ - private static final String MODIFIED = "modified"; - } - - public static ImageIcon getImage(String strFilename) { - Class thisClass = StudioPanel.class; - - java.net.URL url = null; - - if (strFilename.startsWith("/")) - url = thisClass.getResource(strFilename); - else - // Locate the desired image file and create a URL to it - url = thisClass.getResource("/toolbarButtonGraphics/" + strFilename); - - // See if we successfully found the image - if (url == null) - //System.out.println("Unable to load the following image: " + - // strFilename); - return null; - - Toolkit toolkit = Toolkit.getDefaultToolkit(); - Image image = toolkit.getImage(url); - return new ImageIcon(image); - } -} +package studio.ui; + +import java.awt.*; +import java.awt.event.*; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.*; +import java.util.*; +import java.util.List; +import javax.swing.*; +import static javax.swing.JSplitPane.VERTICAL_SPLIT; +import static studio.ui.EscapeDialog.DialogResult.ACCEPTED; +import static studio.ui.EscapeDialog.DialogResult.CANCELLED; + +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.filechooser.FileFilter; +import javax.swing.filechooser.FileView; +import javax.swing.plaf.basic.BasicSplitPaneUI; +import javax.swing.table.TableModel; +import javax.swing.text.*; +import javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; +import kx.c; +import org.netbeans.editor.*; +import org.netbeans.editor.Utilities; +import studio.core.Credentials; +import studio.kdb.ListModel; +import studio.qeditor.QKit; +import org.netbeans.editor.ext.ExtKit; +import org.netbeans.editor.ext.ExtSettingsInitializer; +import studio.qeditor.QSettingsInitializer; +import studio.kdb.*; +import studio.utils.BrowserLaunch; +import studio.utils.OSXAdapter; +import studio.utils.SwingWorker; + +public class StudioPanel extends JPanel implements Observer,WindowListener { + static { + // Register us + LocaleSupport.addLocalizer(new Impl("org.netbeans.editor.Bundle")); + + Settings.addInitializer(new BaseSettingsInitializer(),Settings.CORE_LEVEL); + Settings.addInitializer(new ExtSettingsInitializer(),Settings.CORE_LEVEL); + + QKit editorKit = new QKit(); + JEditorPane.registerEditorKitForContentType(editorKit.getContentType(), + editorKit.getClass().getName()); + Settings.addInitializer(new QSettingsInitializer()); + Settings.reset(); + } + + private JComboBox comboServer; + private JTextField txtServer; + private JTable table; + private String exportFilename; + private String lastQuery = null; + private JMenuBar menubar; + private JToolBar toolbar; + private JEditorPane textArea; + private JSplitPane splitpane; + private JTabbedPane tabbedPane; + private Font font = null; + private UserAction arrangeAllAction; + private UserAction closeFileAction; + private UserAction newFileAction; + private UserAction openFileAction; + private UserAction openInExcel; + private UserAction codeKxComAction; + private UserAction serverListAction; + private UserAction openFileInNewWindowAction; + private UserAction saveFileAction; + private UserAction saveAsFileAction; + private UserAction exportAction; + private UserAction chartAction; + private ActionFactory.UndoAction undoAction; + private ActionFactory.RedoAction redoAction; + private BaseKit.CutAction cutAction; + private BaseKit.CopyAction copyAction; + private BaseKit.PasteAction pasteAction; + private BaseKit.SelectAllAction selectAllAction; + private Action findAction; + private Action replaceAction; + private UserAction stopAction; + private UserAction executeAction; + private UserAction executeCurrentLineAction; + private UserAction refreshAction; + private UserAction aboutAction; + private UserAction exitAction; + private UserAction settingsAction; + private UserAction toggleDividerOrientationAction; + private UserAction minMaxDividerAction; + private UserAction editServerAction; + private UserAction addServerAction; + private UserAction removeServerAction; + private static int scriptNumber = 0; + private static int myScriptNumber; + private JFrame frame; + public static java.util.List windowList = Collections.synchronizedList(new LinkedList()); + private int menuShortcutKeyMask = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); + + private final static int MAX_SERVERS_TO_CLONE = 20; + + public void refreshFrameTitle() { + String s = (String) textArea.getDocument().getProperty("filename"); + if (s == null) + s = "Script" + myScriptNumber; + String title = s.replace('\\','/'); + frame.setTitle(title + (getModified() ? " (not saved) " : "") + (server!=null?" @"+server.toString():"") +" Studio for kdb+ " + Lm.getVersionString()); + } + + public static class WindowListChangedEvent extends EventObject { + public WindowListChangedEvent(Object source) { + super(source); + } + } + + public interface WindowListChangedEventListener extends EventListener { + public void WindowListChangedEventOccurred(WindowListChangedEvent evt); + } + + public static class WindowListMonitor { + protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); + + public synchronized void addEventListener(WindowListChangedEventListener listener) { + listenerList.add(WindowListChangedEventListener.class,listener); + } + + public synchronized void removeEventListener(WindowListChangedEventListener listener) { + listenerList.remove(WindowListChangedEventListener.class,listener); + } + + synchronized void fireMyEvent(WindowListChangedEvent evt) { + Object[] listeners = listenerList.getListenerList(); + for (int i = 0;i < listeners.length;i += 2) + if (listeners[i] == WindowListChangedEventListener.class) + ((WindowListChangedEventListener) listeners[i + 1]).WindowListChangedEventOccurred(evt); + } + } + public static WindowListMonitor windowListMonitor = new WindowListMonitor(); +/* + private void updateKeyBindings(JEditorPane editorPane) { + InputMap inputMap = editorPane.getInputMap(); + inputMap.put(KeyStroke.getKeyStroke("DELETE"),ExtKit.deleteNextCharAction); + inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"),ExtKit.deletePrevCharAction); + inputMap.put(KeyStroke.getKeyStroke("ENTER"),ExtKit.insertBreakAction); + inputMap.put(KeyStroke.getKeyStroke("UP"),ExtKit.upAction); + inputMap.put(KeyStroke.getKeyStroke("DOWN"),ExtKit.downAction); + inputMap.put(KeyStroke.getKeyStroke("LEFT"),ExtKit.backwardAction); + inputMap.put(KeyStroke.getKeyStroke("RIGHT"),ExtKit.forwardAction); + inputMap.put(KeyStroke.getKeyStroke("ctrl Z"),ExtKit.undoAction); + inputMap.put(KeyStroke.getKeyStroke("ctrl Y"),ExtKit.redoAction); + } +*/ + private void updateUndoRedoState(UndoManager um) { + undoAction.setEnabled(um.canUndo()); + redoAction.setEnabled(um.canRedo()); + } + + private void initDocument() { + initActions(); + refreshActionState(); + + Document doc = null; + if (textArea == null) { + textArea = new JEditorPane("text/q",""); + Action[] actions = textArea.getActions(); + + for (int i = 0;i < actions.length;i++) + if (actions[i] instanceof BaseKit.CopyAction) { + copyAction = (BaseKit.CopyAction) actions[i]; + copyAction.putValue(Action.SHORT_DESCRIPTION,"Copy the selected text to the clipboard"); + copyAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "copy.png")); + copyAction.putValue(Action.NAME,I18n.getString("Copy")); + copyAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_C)); + copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_C,menuShortcutKeyMask)); + } + else if (actions[i] instanceof BaseKit.CutAction) { + cutAction = (BaseKit.CutAction) actions[i]; + cutAction.putValue(Action.SHORT_DESCRIPTION,"Cut the selected text"); + cutAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "cut.png")); + cutAction.putValue(Action.NAME,I18n.getString("Cut")); + cutAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_T)); + cutAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_X,menuShortcutKeyMask)); + } + else if (actions[i] instanceof BaseKit.PasteAction) { + pasteAction = (BaseKit.PasteAction) actions[i]; + pasteAction.putValue(Action.SHORT_DESCRIPTION,"Paste text from the clipboard"); + pasteAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "paste.png")); + pasteAction.putValue(Action.NAME,I18n.getString("Paste")); + pasteAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_P)); + pasteAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_V,menuShortcutKeyMask)); + } + else if (actions[i] instanceof ExtKit.FindAction) { + findAction = actions[i]; + findAction.putValue(Action.SHORT_DESCRIPTION,"Find text in the document"); + findAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "find.png")); + findAction.putValue(Action.NAME,I18n.getString("Find")); + findAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_F)); + findAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_F,menuShortcutKeyMask)); + } + else if (actions[i] instanceof ExtKit.ReplaceAction) { + replaceAction = actions[i]; + replaceAction.putValue(Action.SHORT_DESCRIPTION,"Replace text in the document"); + replaceAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "replace.png")); + replaceAction.putValue(Action.NAME,I18n.getString("Replace")); + replaceAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_R)); + replaceAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_R,menuShortcutKeyMask)); + } + else if (actions[i] instanceof BaseKit.SelectAllAction) { + selectAllAction = (BaseKit.SelectAllAction) actions[i]; + selectAllAction.putValue(Action.SHORT_DESCRIPTION,"Select all text in the document"); + selectAllAction.putValue(Action.SMALL_ICON,null); + selectAllAction.putValue(Action.NAME,I18n.getString("SelectAll")); + selectAllAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_A)); + selectAllAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_A,menuShortcutKeyMask)); + } + else if (actions[i] instanceof ActionFactory.UndoAction) { + undoAction = (ActionFactory.UndoAction) actions[i]; + undoAction.putValue(Action.SHORT_DESCRIPTION,"Undo the last change to the document"); + undoAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "undo.png")); + undoAction.putValue(Action.NAME,I18n.getString("Undo")); + undoAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_U)); + undoAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_Z,menuShortcutKeyMask)); + } + else if (actions[i] instanceof ActionFactory.RedoAction) { + redoAction = (ActionFactory.RedoAction) actions[i]; + redoAction.putValue(Action.SHORT_DESCRIPTION,"Redo the last change to the document"); + redoAction.putValue(Action.SMALL_ICON,getImage(Config.imageBase2 + "redo.png")); + redoAction.putValue(Action.NAME,I18n.getString("Redo")); + redoAction.putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_R)); + redoAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_Y,menuShortcutKeyMask)); + } + + doc = textArea.getDocument(); + doc.putProperty("filename",null); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + // doc.putProperty("created", Boolean.TRUE); + } + else + doc = textArea.getDocument(); + + JComponent c = (textArea.getUI() instanceof BaseTextUI) ? Utilities.getEditorUI(textArea).getExtComponent() : new JScrollPane(textArea); + + doc.putProperty("server",server); + + MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); + if (mdl == null) { + mdl = new MarkingDocumentListener(c); + doc.putProperty("MarkingDocumentListener",mdl); + doc.addDocumentListener(mdl); + } + mdl.setModified(false); + + UndoManager um = (UndoManager) doc.getProperty(BaseDocument.UNDO_MANAGER_PROP); + if (um == null) { + um = new UndoManager() { + public void undoableEditHappened(UndoableEditEvent e) { + super.undoableEditHappened(e); + updateUndoRedoState(this); + } + + public synchronized void redo() throws CannotRedoException { + super.redo(); + updateUndoRedoState(this); + } + + public synchronized void undo() throws CannotUndoException { + super.undo(); + updateUndoRedoState(this); + } + }; + doc.putProperty(BaseDocument.UNDO_MANAGER_PROP,um); + doc.addUndoableEditListener(um); + } + um.discardAllEdits(); + updateUndoRedoState(um); + + if (splitpane.getTopComponent() != c) { + splitpane.setTopComponent(c); + splitpane.setDividerLocation(0.5); + } + + rebuildToolbar(); + rebuildMenuBar(); + + textArea.requestFocus(); + } + + private void refreshActionState() { + newFileAction.setEnabled(true); + arrangeAllAction.setEnabled(true); + openFileAction.setEnabled(true); + serverListAction.setEnabled(true); + openFileInNewWindowAction.setEnabled(true); + saveFileAction.setEnabled(true); + saveAsFileAction.setEnabled(true); + exportAction.setEnabled(false); + chartAction.setEnabled(false); + openInExcel.setEnabled(false); + stopAction.setEnabled(false); + executeAction.setEnabled(true); + executeCurrentLineAction.setEnabled(true); + refreshAction.setEnabled(false); + +// helpAction.setEnabled(true); + aboutAction.setEnabled(true); + exitAction.setEnabled(true); + settingsAction.setEnabled(true); + } + + private String getFilename() { + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + FileFilter ff = + new FileFilter() { + public String getDescription() { + return "q script"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".q")) + return true; + else + return false; + } + }; + + chooser.addChoosableFileFilter(ff); + + chooser.setFileFilter(ff); + + String filename = (String) textArea.getDocument().getProperty("filename"); + if (filename != null) { + File file = new File(filename); + File dir = new File(file.getPath()); + chooser.setCurrentDirectory(dir); + } + + int option = chooser.showOpenDialog(textArea); + + if (option == JFileChooser.APPROVE_OPTION) { + File sf = chooser.getSelectedFile(); + File f = chooser.getCurrentDirectory(); + String dir = f.getAbsolutePath(); + + try { + filename = dir + "/" + sf.getName(); + return filename; + } + catch (Exception e) { + } + } + + return null; + } + + private void exportAsExcel(final String filename) { + new ExcelExporter().exportTableX(frame,table,new File(filename),false); + } + + private void exportAsDelimited(final TableModel model,final String filename,final char delimiter) { + final String message = "Exporting data to " + filename; + + final String note = "0% complete"; + + String title = "Studio for kdb+"; + UIManager.put("ProgressMonitor.progressText",title); + + final int min = 0; + final int max = 100; + final ProgressMonitor pm = new ProgressMonitor(frame,message,note,min,max); + pm.setMillisToDecideToPopup(100); + pm.setMillisToPopup(100); + pm.setProgress(0); + + Runnable runner = new Runnable() { + public void run() { + if (filename != null) { + String lineSeparator = System.getProperty("line.separator");; + + BufferedWriter fw; + + try { + fw = new BufferedWriter(new FileWriter(filename)); + + for (int col = 0;col < model.getColumnCount();col++) { + if (col > 0) + fw.write(delimiter); + + fw.write(model.getColumnName(col)); + } + fw.write(lineSeparator); + + int maxRow = model.getRowCount(); + int lastProgress = 0; + + for (int r = 1;r <= maxRow;r++) { + for (int col = 0;col < model.getColumnCount();col++) { + if (col > 0) + fw.write(delimiter); + + K.KBase o = (K.KBase) model.getValueAt(r - 1,col); + if (!o.isNull()) + fw.write(o.toString(false)); + } + fw.write(lineSeparator); + + boolean cancelled = pm.isCanceled(); + + if (cancelled) + break; + else { + final int progress = (100 * r) / maxRow; + if (progress > lastProgress) { + final String note = "" + progress + "% complete"; + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + pm.setProgress(progress); + pm.setNote(note); + } + }); + + Thread.yield(); + } + } + } + + fw.close(); + } + catch (FileNotFoundException ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + catch (IOException ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + catch (Exception ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + finally { + pm.close(); + } + } + } + }; + + Thread t = new Thread(runner); + t.setName("export"); + t.setPriority(Thread.MIN_PRIORITY); + t.start(); + } + + private void exportAsXml(final TableModel model,final String filename) { + final String message = "Exporting data to " + filename; + + final String note = "0% complete"; + + String title = "Studio for kdb+"; + UIManager.put("ProgressMonitor.progressText",title); + + final int min = 0; + final int max = 100; + final ProgressMonitor pm = new ProgressMonitor(frame,message,note,min,max); + pm.setMillisToDecideToPopup(100); + pm.setMillisToPopup(100); + pm.setProgress(0); + + Runnable runner = new Runnable() { + public void run() { + if (filename != null) { + String lineSeparator = System.getProperty("line.separator");; + + BufferedWriter fw = null; + + try { + fw = new BufferedWriter(new FileWriter(filename)); + + fw.write(""); + + int maxRow = model.getRowCount(); + int lastProgress = 0; + + fw.write(lineSeparator); + + String[] columns = new String[model.getColumnCount()]; + for (int col = 0;col < model.getColumnCount();col++) + columns[col] = model.getColumnName(col); + + for (int r = 1;r <= maxRow;r++) { + fw.write(""); + for (int col = 0;col < columns.length;col++) { + fw.write("<" + columns[col] + ">"); + + K.KBase o = (K.KBase) model.getValueAt(r - 1,col); + if (!o.isNull()) + fw.write(o.toString(false)); + + fw.write(""); + } + fw.write(""); + fw.write(lineSeparator); + + boolean cancelled = pm.isCanceled(); + + if (cancelled) + break; + else { + final int progress = (100 * r) / maxRow; + if (progress > lastProgress) { + final String note = "" + progress + "% complete"; + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + pm.setProgress(progress); + pm.setNote(note); + } + }); + + Thread.yield(); + } + } + } + fw.write(""); + + fw.close(); + } + catch (FileNotFoundException ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + catch (IOException ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + catch (Exception ex) { + ex.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + finally { + pm.close(); + } + } + } + }; + + Thread t = new Thread(runner); + t.setName("export"); + t.setPriority(Thread.MIN_PRIORITY); + t.start(); + } + + private void exportAsTxt(String filename) { + exportAsDelimited(table.getModel(),filename,'\t'); + } + + private void exportAsCSV(String filename) { + exportAsDelimited(table.getModel(),filename,','); + } + + private void export() { + JFileChooser chooser = new JFileChooser(); + chooser.setDialogType(JFileChooser.SAVE_DIALOG); + chooser.setDialogTitle("Export result set as"); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + FileFilter csvFilter = null; + FileFilter txtFilter = null; + FileFilter xmlFilter = null; + FileFilter xlsFilter = null; + + if (table != null) { + csvFilter = + new FileFilter() { + public String getDescription() { + return "csv (Comma delimited)"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".csv")) + return true; + else + return false; + } + }; + + txtFilter = + new FileFilter() { + public String getDescription() { + return "txt (Tab delimited)"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".txt")) + return true; + else + return false; + } + }; + + xmlFilter = + new FileFilter() { + public String getDescription() { + return "xml"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".xml")) + return true; + else + return false; + } + }; + + + xlsFilter = + new FileFilter() { + public String getDescription() { + return "xls (Microsoft Excel)"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".xls")) + return true; + else + return false; + } + }; + + chooser.addChoosableFileFilter(csvFilter); + chooser.addChoosableFileFilter(txtFilter); + chooser.addChoosableFileFilter(xmlFilter); + chooser.addChoosableFileFilter(xlsFilter); + } + + if (exportFilename != null) { + File file = new File(exportFilename); + File dir = new File(file.getPath()); + chooser.setCurrentDirectory(dir); + chooser.ensureFileIsVisible(file); + if (table != null) + if (exportFilename.endsWith(".xls")) + chooser.setFileFilter(xlsFilter); + else if (exportFilename.endsWith(".csv")) + chooser.setFileFilter(csvFilter); + else if (exportFilename.endsWith(".xml")) + chooser.setFileFilter(xmlFilter); + else if (exportFilename.endsWith(".txt")) + chooser.setFileFilter(txtFilter); + } + + int option = chooser.showSaveDialog(textArea); + + if (option == JFileChooser.APPROVE_OPTION) { + File sf = chooser.getSelectedFile(); + File f = chooser.getCurrentDirectory(); + String dir = f.getAbsolutePath(); + +// Cursor cursor= frame.getCursor(); + + try { + // frame.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)); + FileFilter ff = chooser.getFileFilter(); + + exportFilename = dir + "/" + sf.getName(); + + if (table != null) + if (exportFilename.endsWith(".xls")) + exportAsExcel(exportFilename); + else if (exportFilename.endsWith(".csv")) + exportAsCSV(exportFilename); + else if (exportFilename.endsWith(".txt")) + exportAsTxt(exportFilename); + else if (exportFilename.endsWith(".xml")) + exportAsXml(table.getModel(),exportFilename); + /* else if (exportFilename.endsWith(".res")) { + exportAsBin(exportFilename); + } + */ + else + if (ff == csvFilter) + exportAsCSV(exportFilename); + else if (ff == xlsFilter) + exportAsExcel(exportFilename); + else if (ff == txtFilter) + exportAsTxt(exportFilename); + else if (ff == xmlFilter) + exportAsXml(table.getModel(),exportFilename); + /*else if( ff == binFilter){ + exportAsBin(exportFilename); + } + */ + else + JOptionPane.showMessageDialog(frame, + "Warning", + "You did not specify what format to export the file as.\n Cancelling data export", + JOptionPane.WARNING_MESSAGE, + getImage(Config.imageBase + "32x32/warning.png")); + /* else { + exportAsBin(exportFilename); + } + */ + } + catch (Exception e) { + JOptionPane.showMessageDialog(frame, + "Error", + "An error occurred whilst writing the export file.\n Details are: " + e.getMessage(), + JOptionPane.ERROR_MESSAGE, + getImage(Config.imageBase + "32x32/error.png")); + } + finally { + // frame.setCursor(cursor); + } + } + } + + public void newFile() { + try { + String filename = (String) textArea.getDocument().getProperty("filename"); + if (!saveIfModified(filename)) + return; + + textArea.getDocument().remove(0,textArea.getDocument().getLength()); + textArea.getDocument().putProperty("filename",null); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + initDocument(); + refreshFrameTitle(); + } + catch (BadLocationException ex) { + ex.printStackTrace(); + } + } + + public void openFile() { + String filename = (String) textArea.getDocument().getProperty("filename"); + if (!saveIfModified(filename)) + return; + + filename = getFilename(); + + if (filename != null) { + loadFile(filename); + addToMruFiles(filename); + } + } + // returns true to continue + public boolean saveIfModified(String filename) { + if (getModified()) { + int choice = JOptionPane.showOptionDialog(frame, + "Changes not saved.\nSave now?", + "Save changes?", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + getImage(Config.imageBase + "32x32/question.png"), + null, // use standard button titles + null); // no default selection + + if (choice == JOptionPane.YES_OPTION) { + try { + if (saveFile(filename,false)) + // was cancelled so return + return false; + } + catch (Exception e) { + return false; + } + return true; + } + else if ((choice == JOptionPane.CANCEL_OPTION) || (choice == JOptionPane.CLOSED_OPTION)) + return false; + } + return true; + } + + public void loadMRUFile(String filename,String oldFilename) { + if (!saveIfModified(oldFilename)) + return; + + loadFile(filename); + addToMruFiles(filename); + setServer(server); + } + + private void addToMruFiles(String filename) { + if (filename == null) + return; + + Vector v = new Vector(); + v.add(filename); + String[] mru = Config.getInstance().getMRUFiles(); + for (int i = 0;i < mru.length;i++) + if (!v.contains(mru[i])) + v.add(mru[i]); + Config.getInstance().saveMRUFiles((String[]) v.toArray(new String[0])); + rebuildMenuBar(); + } + + static public String getContents(File aFile) { + StringBuffer contents = new StringBuffer(); + + try { + InputStreamReader isr = new InputStreamReader(new FileInputStream(aFile), + "UTF-8"); + BufferedReader input = new BufferedReader(isr); + try { + + String line = null; + while ((line = input.readLine()) != null) { + contents.append(line); + contents.append(System.getProperty("line.separator")); + } + } + finally { + input.close(); + } + } + catch (IOException ex) { + ex.printStackTrace(); + } + + return contents.toString(); + } + + public void loadFile(String filename) { + try { + String s = getContents(new File(filename)); + + textArea.getDocument().remove(0,textArea.getDocument().getLength()); + textArea.getDocument().insertString(0,s,null); + textArea.getDocument().putProperty("filename",filename); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + initDocument(); + textArea.setCaretPosition(0); + refreshFrameTitle(); + } + catch (BadLocationException ex) { + ex.printStackTrace(); + } + } + + public boolean saveAsFile() { + JFileChooser chooser = new JFileChooser(); + chooser.setDialogType(JFileChooser.SAVE_DIALOG); + chooser.setDialogTitle("Save script as"); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + + FileFilter ff = + new FileFilter() { + public String getDescription() { + return "q script"; + } + + public boolean accept(File file) { + if (file.isDirectory() || file.getName().endsWith(".q")) + return true; + else + return false; + } + }; + + chooser.addChoosableFileFilter(ff); + + chooser.setFileFilter(ff); + + String filename = (String) textArea.getDocument().getProperty("filename"); + if (filename != null) { + File file = new File(filename); + File dir = new File(file.getPath()); + chooser.setCurrentDirectory(dir); + } + +// chooser.setMultiSelectionEnabled(true); + int option = chooser.showSaveDialog(textArea); + + if (option == JFileChooser.APPROVE_OPTION) { + File sf = chooser.getSelectedFile(); + File f = chooser.getCurrentDirectory(); + String dir = f.getAbsolutePath(); + + try { + filename = dir + "/" + sf.getName(); + sf = new File(filename); + + if (sf.exists()) { + int choice = JOptionPane.showOptionDialog(frame, + filename + " already exists.\nOverwrite?", + "Overwrite?", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + getImage(Config.imageBase + "32x32/question.png"), + null, // use standard button titles + null); // no default selection + + if (choice != JOptionPane.YES_OPTION) + return false; + } + + return saveFile(filename,true); + } + catch (Exception e) { + } + } + return false; + } + // private boolean wasLoaded=false; + // returns true if saved, false if error or cancelled + public boolean saveFile(String filename,boolean force) { + if (filename == null) + return saveAsFile(); + + try { + if (!force) + if (null == textArea.getDocument().getProperty("filename")) + return saveAsFile(); + + textArea.write(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"))); + textArea.getDocument().putProperty("filename",filename); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + setModified(false); + addToMruFiles(filename); + refreshFrameTitle(); + return true; + } + catch (Exception e) { + } + + return false; + } + + private void arrangeAll() { + int noWins = windowList.size(); + + Iterator i = windowList.iterator(); + + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + + int noRows = noWins > 3 ? 3 : noWins; + int height = screenSize.height / noRows; + + for (int row = 0;row < noRows;row++) { + int noCols = (noWins / 3); + + if ((row == 0) && ((noWins % 3) > 0)) + noCols++; + else if ((row == 1) && ((noWins % 3) > 1)) + noCols++; + + int width = screenSize.width / noCols; + + for (int col = 0;col < noCols;col++) { + Object o = i.next(); + JFrame f; + + if (o instanceof StudioPanel) + f = ((StudioPanel) o).frame; + else + f = (JFrame) o; + + f.setSize(width,height); + f.setLocation(col * width,((noRows - 1) - row) * height); + ensureDeiconified(f); + } + } + } + + private void setModified(boolean value) { + if (textArea != null) { + Document doc = textArea.getDocument(); + + if (doc != null) { + MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); + if (mdl != null) + mdl.setModified(value); + } + } + } + + private boolean getModified() { + if (textArea != null) { + Document doc = textArea.getDocument(); + + if (doc != null) { + MarkingDocumentListener mdl = (MarkingDocumentListener) doc.getProperty("MarkingDocumentListener"); + if (mdl != null) + return mdl.getModified(); + } + } + + return true; + } + + private void setServer(Server server) { + if (server == null) + return; + + this.server = server; + + if (textArea != null) { + Document doc = textArea.getDocument(); + + if (doc != null) + doc.putProperty("server",server); + Utilities.getEditorUI(textArea).getComponent().setBackground(server.getBackgroundColor()); + } + + new ReloadQKeywords(server); + Config.getInstance().setLRUServer(server); + + refreshFrameTitle(); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + } + + private void initActions() { + newFileAction = new UserAction(I18n.getString("New"), + getImage(Config.imageBase2 + "document_new.png"), + "Create a blank script", + new Integer(KeyEvent.VK_N), + null) { + public void actionPerformed(ActionEvent e) { + // PrintUtilities.printComponent(textArea); + newFile(); + } + }; + + arrangeAllAction = new UserAction(I18n.getString("ArrangeAll"), + getImage(Config.imageBase2 + "blank.png"), + "Arrange all windows on screen", + new Integer(KeyEvent.VK_A), + null) { + public void actionPerformed(ActionEvent e) { + arrangeAll(); + } + }; + + minMaxDividerAction = new UserAction(I18n.getString("MaximizeEditorPane"), + getImage(Config.imageBase2 + "blank.png"), + "Maximize editor pane", + new Integer(KeyEvent.VK_M), + KeyStroke.getKeyStroke(KeyEvent.VK_M,menuShortcutKeyMask)) { + public void actionPerformed(ActionEvent e) { + minMaxDivider(); + } + }; + + toggleDividerOrientationAction = new UserAction(I18n.getString("ToggleDividerOrientation"), + getImage(Config.imageBase2 + "blank.png"), + "Toggle the window divider's orientation", + new Integer(KeyEvent.VK_C), + null) { + public void actionPerformed(ActionEvent e) { + toggleDividerOrientation(); + } + }; + + closeFileAction = new UserAction(I18n.getString("Close"), + getImage(Config.imageBase2 + "blank.png"), + "Close current document", + new Integer(KeyEvent.VK_C), + null) { + public void actionPerformed(ActionEvent e) { + quitWindow(); + if (windowList.size() == 0) + System.exit(0); + } + }; + + openFileAction = new UserAction(I18n.getString("Open"), + getImage(Config.imageBase2 + "folder.png"), + "Open a script", + new Integer(KeyEvent.VK_O), + KeyStroke.getKeyStroke(KeyEvent.VK_O,menuShortcutKeyMask)) { + public void actionPerformed(ActionEvent e) { + openFile(); + } + }; + + openFileInNewWindowAction = new UserAction(I18n.getString("NewWindow"), + getImage(Config.imageBase2 + "blank.png"), + "Open a new window", + new Integer(KeyEvent.VK_N), + KeyStroke.getKeyStroke(KeyEvent.VK_N, menuShortcutKeyMask) ) { + public void actionPerformed(ActionEvent e) { + new StudioPanel(server,null); + } + }; + + serverListAction = new UserAction(I18n.getString("ServerList"), + getImage(Config.imageBase + "text_tree.png"), + "Show sever list", + new Integer(KeyEvent.VK_L), + KeyStroke.getKeyStroke(KeyEvent.VK_L, menuShortcutKeyMask | Event.SHIFT_MASK) ) { + public void actionPerformed(ActionEvent e) { + ServerList serverList = new ServerList(frame, Config.getInstance().getServers(), server); + serverList.alignAndShow(); + Server selectedServer = serverList.getSelectedServer(); + if (selectedServer.equals(server)) return; + + setServer(selectedServer); + rebuildToolbar(); + } + }; + + editServerAction = new UserAction(I18n.getString("Edit"), + getImage(Config.imageBase2 + "server_information.png"), + "Edit the server details", + new Integer(KeyEvent.VK_E), + null) { + public void actionPerformed(ActionEvent e) { + Server s = new Server(server); + + EditServerForm f = new EditServerForm(frame,s); + f.alignAndShow(); + if (f.getResult() == ACCEPTED) { + if (stopAction.isEnabled()) + stopAction.actionPerformed(e); + + ConnectionPool.getInstance().purge(server); + Config.getInstance().removeServer(server); + + s = f.getServer(); + Config.getInstance().addServer(s); + setServer(s); + rebuildToolbar(); + rebuildMenuBar(); + + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + } + } + }; + + + addServerAction = new UserAction(I18n.getString("Add"), + getImage(Config.imageBase2 + "server_add.png"), + "Configure a new server", + new Integer(KeyEvent.VK_A), + null) { + public void actionPerformed(ActionEvent e) { + AddServerForm f = new AddServerForm(frame); + f.alignAndShow(); + if (f.getResult() == ACCEPTED) { + Server s = f.getServer(); + Config.getInstance().addServer(s); + ConnectionPool.getInstance().purge(s); //? + setServer(s); + rebuildToolbar(); + rebuildMenuBar(); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + } + } + }; + + removeServerAction = new UserAction(I18n.getString("Remove"), + getImage(Config.imageBase2 + "server_delete.png"), + "Remove this server", + new Integer(KeyEvent.VK_R), + null) { + public void actionPerformed(ActionEvent e) { + int choice = JOptionPane.showOptionDialog(frame, + "Remove server " + server.getName() + " from list?", + "Remove server?", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + getImage(Config.imageBase + "32x32/question.png"), + null, // use standard button titles + null); // no default selection + + if (choice == 0) { + Config.getInstance().removeServer(server); + + Server[] servers = Config.getInstance().getServers(); + + if (servers.length > 0) + setServer(servers[0]); + + rebuildToolbar(); + rebuildMenuBar(); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + } + } + }; + + + saveFileAction = new UserAction(I18n.getString("Save"), + getImage(Config.imageBase2 + "disks.png"), + "Save the script", + new Integer(KeyEvent.VK_S), + KeyStroke.getKeyStroke(KeyEvent.VK_S,menuShortcutKeyMask)) { + public void actionPerformed(ActionEvent e) { + String filename = (String) textArea.getDocument().getProperty("filename"); + saveFile(filename,false); + } + }; + + saveAsFileAction = new UserAction(I18n.getString("SaveAs"), + getImage(Config.imageBase2 + "save_as.png"), + "Save script as", + new Integer(KeyEvent.VK_A), + null) { + public void actionPerformed(ActionEvent e) { + saveAsFile(); + } + }; + + exportAction = new UserAction(I18n.getString("Export"), + getImage(Config.imageBase2 + "export2.png"), + "Export result set", + new Integer(KeyEvent.VK_E), + null) { + public void actionPerformed(ActionEvent e) { + export(); + } + }; + + chartAction = new UserAction(I18n.getString("Chart"), + Util.getImage(Config.imageBase2 + "chart.png"), + "Chart current data set", + new Integer(KeyEvent.VK_E), + null) { + public void actionPerformed(ActionEvent e) { + new LineChart((KTableModel) table.getModel()); + //new PriceVolumeChart(table); + } + }; + + + stopAction = new UserAction(I18n.getString("Stop"), + getImage(Config.imageBase2 + "stop.png"), + "Stop the query", + new Integer(KeyEvent.VK_S), + null) { + public void actionPerformed(ActionEvent e) { + if (worker != null) { + worker.interrupt(); + stopAction.setEnabled(false); + textArea.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + } + } + }; + + + openInExcel = new UserAction(I18n.getString("OpenInExcel"), + getImage(Config.imageBase + "excel_icon.gif"), + "Open in Excel", + new Integer(KeyEvent.VK_O), + null) { + + public void actionPerformed(ActionEvent e) { + try { + File file = File.createTempFile("studioExport",".xls"); + new ExcelExporter().exportTableX(frame,table,file,true); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + }; + + + executeAction = new UserAction(I18n.getString("Execute"), + Util.getImage(Config.imageBase2 + "table_sql_run.png"), + "Execute the full or highlighted text as a query", + new Integer(KeyEvent.VK_E), + KeyStroke.getKeyStroke(KeyEvent.VK_E,menuShortcutKeyMask)) { + + public void actionPerformed(ActionEvent e) { + executeQuery(); + } + }; + + + executeCurrentLineAction = new UserAction(I18n.getString("ExecuteCurrentLine"), + Util.getImage(Config.imageBase2 + "element_run.png"), + "Execute the current line as a query", + new Integer(KeyEvent.VK_ENTER), + KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,menuShortcutKeyMask)) { + + public void actionPerformed(ActionEvent e) { + executeQueryCurrentLine(); + } + }; + + + refreshAction = new UserAction(I18n.getString("Refresh"), + getImage(Config.imageBase2 + "refresh.png"), + "Refresh the result set", + new Integer(KeyEvent.VK_R), + KeyStroke.getKeyStroke(KeyEvent.VK_Y,menuShortcutKeyMask | Event.SHIFT_MASK)) { + + public void actionPerformed(ActionEvent e) { + refreshQuery(); + } + }; + + aboutAction = new UserAction(I18n.getString("About"), + Util.getImage(Config.imageBase2 + "about.png"), + "About Studio for kdb+", + new Integer(KeyEvent.VK_E), + null) { + + public void actionPerformed(ActionEvent e) { + about(); + } + }; + + exitAction = new UserAction(I18n.getString("Exit"), + getImage(Config.imageBase2 + "blank.png"), + "Close this window", + new Integer(KeyEvent.VK_X), + null) { + + public void actionPerformed(ActionEvent e) { + if (quit()) + System.exit(0); + } + }; + + settingsAction = new UserAction("Settings", + getImage(Config.imageBase2 + "blank.png"), + "Settings", + new Integer(KeyEvent.VK_S), + null) { + + public void actionPerformed(ActionEvent e) { + settings(); + } + }; + + codeKxComAction = new UserAction("code.kx.com", + Util.getImage(Config.imageBase2 + "text.png"), + "Open code.kx.com", + new Integer(KeyEvent.VK_C), + null) { + + public void actionPerformed(ActionEvent e) { + try { + BrowserLaunch.openURL("http://code.kx.com/trac/wiki/Reference"); + } catch (Exception ex) { + JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + ex.getLocalizedMessage()); + } + } + }; + } + + public void settings() { + SettingsDialog dialog = new SettingsDialog(frame); + dialog.alignAndShow(); + if (dialog.getResult() == CANCELLED) return; + + String auth = dialog.getDefaultAuthenticationMechanism(); + Config.getInstance().setDefaultAuthMechanism(auth); + Config.getInstance().setDefaultCredentials(auth, new Credentials(dialog.getUser(), dialog.getPassword())); + } + + public void about() { + HelpDialog help = new HelpDialog(frame); + Util.centerChildOnParent(help,frame); + // help.setTitle("About Studio for kdb+"); + help.pack(); + help.setVisible(true); + } + + public boolean quit() { + boolean okToExit = true; + + Object[] objs = windowList.toArray(); + + for (int i = 0;i < objs.length;i++) { + Object o = objs[i]; + + if (o instanceof StudioPanel) { + if (!((StudioPanel) o).quitWindow()) + okToExit = false; + } + else + if (o instanceof JFrame) { + JFrame f = (JFrame) o; + f.setVisible(false); + + f.dispose(); + } + } + + return okToExit; + } + + public boolean quitWindow() { + if (getModified()) { + int choice = JOptionPane.showOptionDialog(frame, + "Changes not saved.\nSave now?", + "Save changes?", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + getImage(Config.imageBase + "32x32/question.png"), + null, // use standard button titles + null); // no default selection + + if (choice == 0) + try { + String filename = (String) textArea.getDocument().getProperty("filename"); + if (!saveFile(filename,false)) + // was cancelled so return + return false; + } + catch (Exception e) { + return false; + } + else if ((choice == 2) || (choice == JOptionPane.CLOSED_OPTION)) + return false; + } + + windowList.remove(this); + windowListMonitor.removeEventListener(windowListChangedEventListener); + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + frame.dispose(); + + return true; + } + + private void rebuildMenuBar() { + menubar = createMenuBar(); + SwingUtilities.invokeLater( + new Runnable() { + + public void run() { + if (frame != null) { + frame.setJMenuBar(menubar); + menubar.validate(); + menubar.repaint(); + frame.validate(); + frame.repaint(); + } + } + }); + } + + private JMenuBar createMenuBar() { + JMenuBar menubar = new JMenuBar(); + JMenu menu = new JMenu(I18n.getString("File")); + menu.setMnemonic(KeyEvent.VK_F); + menu.add(new JMenuItem(newFileAction)); + menu.add(new JMenuItem(openFileAction)); + menu.add(new JMenuItem(saveFileAction)); + menu.add(new JMenuItem(saveAsFileAction)); + + menu.add(new JMenuItem(closeFileAction)); + + if (!MAC_OS_X) { + menu.add(new JMenuItem(settingsAction)); + } + menu.addSeparator(); +// menu.add(new JMenuItem(importAction)); + menu.add(new JMenuItem(openInExcel)); + menu.addSeparator(); + menu.add(new JMenuItem(exportAction)); + menu.addSeparator(); + menu.add(new JMenuItem(chartAction)); + + String[] mru = Config.getInstance().getMRUFiles(); + + if (mru.length > 0) { + menu.addSeparator(); + char[] mnems = "123456789".toCharArray(); + + for (int i = 0;i < (mru.length > mnems.length ? mnems.length : mru.length);i++) { + final String filename = mru[i]; + + JMenuItem item = new JMenuItem("" + (i + 1) + " " + filename); + item.setMnemonic(mnems[i]); + item.setIcon(getImage(Config.imageBase2 + "blank.png")); + item.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + loadMRUFile(filename,(String) textArea.getDocument().getProperty("filename")); + } + }); + menu.add(item); + } + } + + if (!MAC_OS_X) { + menu.addSeparator(); + menu.add(new JMenuItem(exitAction)); + } + menubar.add(menu); + + menu = new JMenu(I18n.getString("Edit")); + menu.setMnemonic(KeyEvent.VK_E); + menu.add(new JMenuItem(undoAction)); + menu.add(new JMenuItem(redoAction)); + menu.addSeparator(); + menu.add(new JMenuItem(cutAction)); + menu.add(new JMenuItem(copyAction)); + menu.add(new JMenuItem(pasteAction)); + menu.addSeparator(); + menu.add(new JMenuItem(selectAllAction)); + menu.addSeparator(); + menu.add(new JMenuItem(findAction)); + menu.add(new JMenuItem(replaceAction)); +// menu.addSeparator(); +// menu.add(new JMenuItem(editFontAction)); + menubar.add(menu); + + menu = new JMenu(I18n.getString("Server")); + menu.setMnemonic(KeyEvent.VK_S); + menu.add(new JMenuItem(addServerAction)); + menu.add(new JMenuItem(editServerAction)); + menu.add(new JMenuItem(removeServerAction)); + + Server[] servers = Config.getInstance().getServers(); + if (servers.length > 0) { + JMenu subMenu = new JMenu(I18n.getString("Clone")); + subMenu.setIcon(Util.getImage(Config.imageBase2 + "data_copy.png")); + + int count = MAX_SERVERS_TO_CLONE; + for (int i = 0;i < servers.length;i++) { + final Server s = servers[i]; + if (!s.equals(server) && count <= 0) continue; + count--; + JMenuItem item = new JMenuItem(s.getName()); + item.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + Server clone = new Server(s); + clone.setName("Clone of " + clone.getName()); + + EditServerForm f = new EditServerForm(frame,clone); + f.alignAndShow(); + + if (f.getResult() == ACCEPTED) { + clone = f.getServer(); + Config.getInstance().addServer(clone); + //ebuildToolbar(); + setServer(clone); + ConnectionPool.getInstance().purge(clone); //? + windowListMonitor.fireMyEvent(new WindowListChangedEvent(this)); + } + } + }); + + subMenu.add(item); + } + + menu.add(subMenu); + } + + menubar.add(menu); + + menu = new JMenu(I18n.getString("Query")); + menu.setMnemonic(KeyEvent.VK_Q); + menu.add(new JMenuItem(executeCurrentLineAction)); + menu.add(new JMenuItem(executeAction)); + menu.add(new JMenuItem(stopAction)); + menu.add(new JMenuItem(refreshAction)); + menubar.add(menu); + + menu = new JMenu(I18n.getString("Window")); + menu.setMnemonic(KeyEvent.VK_W); + + menu.add(new JMenuItem(minMaxDividerAction)); + menu.add(new JMenuItem(toggleDividerOrientationAction)); + menu.add(new JMenuItem(openFileInNewWindowAction)); + menu.add(new JMenuItem(arrangeAllAction)); + menu.add(new JMenuItem(serverListAction)); + + if (windowList.size() > 0) { + menu.addSeparator(); + + int i = 0; + Iterator it = windowList.iterator(); + + while (it.hasNext()) { + String t = "unknown"; + + final Object o = it.next(); + + if (o instanceof StudioPanel) { + StudioPanel r = (StudioPanel) o; + String filename = (String) r.textArea.getDocument().getProperty("filename"); + + if (filename != null) + t = filename.replace('\\','/'); + + if (r.server != null) + t = t + "[" + r.server.getName() + "]"; + else + t = t + "[no server]"; + } + else + if (o instanceof JFrame) + t = ((JFrame) o).getTitle(); + + JMenuItem item = new JMenuItem("" + (i + 1) + " " + t); + item.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + if (o instanceof StudioPanel) { + JFrame f = ((StudioPanel) o).frame; + ensureDeiconified(f); + } + else + ensureDeiconified((JFrame) o); + } + }); + + if (o == this) + item.setIcon(getImage(Config.imageBase2 + "check2.png")); + else + item.setIcon(getImage(Config.imageBase2 + "blank.png")); + + menu.add(item); + i++; + } + } + menubar.add(menu); + menu = new JMenu(I18n.getString("Help")); + menu.setMnemonic(KeyEvent.VK_H); + menu.add(new JMenuItem(codeKxComAction)); + if (!MAC_OS_X) + menu.add(new JMenuItem(aboutAction)); + menubar.add(menu); + + return menubar; + } + + private void ensureDeiconified(JFrame f) { + int state = f.getExtendedState(); + state = state & ~Frame.ICONIFIED; + f.setExtendedState(state); + f.show(); + } + + private void selectConnectionString() { + String connection = txtServer.getText().trim(); + if (connection.length() == 0) return; + if (server != null && server.getConnectionString(false).equals(connection)) return; + + try { + setServer(Config.getInstance().getServerByConnectionString(connection)); + + rebuildToolbar(); + toolbar.validate(); + toolbar.repaint(); + } catch (IllegalArgumentException e) { + refreshConnection(); + } + } + + private void selectServerName() { + String selection = comboServer.getSelectedItem().toString(); + if(! Config.getInstance().getServerNames().contains(selection)) return; + + setServer(Config.getInstance().getServer(selection)); + rebuildToolbar(); + toolbar.validate(); + toolbar.repaint(); + } + + private void refreshConnection() { + if (server == null) { + txtServer.setText(""); + txtServer.setToolTipText("Select connection details"); + } else { + txtServer.setText(server.getConnectionString(false)); + txtServer.setToolTipText(server.getConnectionString(true)); + } + } + + private void toolbarAddServerSelection() { + List names = Config.getInstance().getServerNames(); + String name = server == null ? "" : server.getName(); + if (!names.contains(name)) { + List newNames = new ArrayList<>(); + newNames.add(name); + newNames.addAll(names); + names = newNames; + } + comboServer = new JComboBox<>(names.toArray(new String[0])); + comboServer.setToolTipText("Select the server context"); + comboServer.setSelectedItem(name); + comboServer.addActionListener(e->selectServerName()); + + txtServer = new JTextField(); + txtServer.addActionListener(e -> selectConnectionString()); + txtServer.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(FocusEvent e) { + selectConnectionString(); + } + }); + refreshConnection(); + + toolbar.add(new JLabel(I18n.getString("Server"))); + toolbar.add(comboServer); + toolbar.add(txtServer); + toolbar.add(serverListAction); + toolbar.addSeparator(); + } + + private void rebuildToolbar() { + if (toolbar != null) { + toolbar.removeAll(); + toolbarAddServerSelection(); + if (server == null) { + addServerAction.setEnabled(true); + editServerAction.setEnabled(false); + removeServerAction.setEnabled(false); + stopAction.setEnabled(false); + executeAction.setEnabled(false); + executeCurrentLineAction.setEnabled(false); + refreshAction.setEnabled(false); + } + else { + executeAction.setEnabled(true); + executeCurrentLineAction.setEnabled(true); + editServerAction.setEnabled(true); + removeServerAction.setEnabled(true); + } + + toolbar.add(stopAction); + toolbar.add(executeAction); + toolbar.add(refreshAction); + toolbar.addSeparator(); + + toolbar.add(openFileAction); + toolbar.add(saveFileAction); + toolbar.add(saveAsFileAction); + toolbar.addSeparator(); +// toolbar.add(importAction); + toolbar.add(openInExcel); + toolbar.addSeparator(); + toolbar.add(exportAction); + toolbar.addSeparator(); + + toolbar.add(chartAction); + toolbar.addSeparator(); + + toolbar.add(undoAction); + toolbar.add(redoAction); + toolbar.addSeparator(); + + toolbar.add(cutAction); + toolbar.add(copyAction); + toolbar.add(pasteAction); + + toolbar.addSeparator(); + toolbar.add(findAction); + + toolbar.add(replaceAction); + + toolbar.addSeparator(); + toolbar.add(codeKxComAction); + + for (int j = 0;j < toolbar.getComponentCount();j++) { + Component c = toolbar.getComponentAtIndex(j); + + if (c instanceof JButton) + ((JButton) c).setRequestFocusEnabled(false); + } + } + } + + private JToolBar createToolbar() { + toolbar = new JToolBar(); + toolbar.setFloatable(false); + rebuildToolbar(); + return toolbar; + } + + private static class Impl extends FileView implements + LocaleSupport.Localizer { + // FileView implementation + + public String getName(File f) { + return null; + } + + + public String getDescription(File f) { + return null; + } + + + public String getTypeDescription(File f) { + return null; + } + + + public Boolean isTraversable(File f) { + return null; + } + + + public Icon getIcon(File f) { + if (f.isDirectory()) + return null; + // KitInfo ki = KitInfo.getKitInfoForFile(f); + // return ki == null ? null : ki.getIcon(); + return null; + } + private ResourceBundle bundle; + + public Impl(String bundleName) { + bundle = ResourceBundle.getBundle(bundleName); + } + // Localizer + + public String getString(String key) { + return bundle.getString(key); + } + } + private WindowListChangedEventListener windowListChangedEventListener; + + private int dividerLastPosition; // updated from property change listener + private void minMaxDivider(){ + //BasicSplitPaneDivider divider = ((BasicSplitPaneUI)splitpane.getUI()).getDivider(); + //((JButton)divider.getComponent(0)).doClick(); + //((JButton)divider.getComponent(1)).doClick(); + if(splitpane.getDividerLocation()>=splitpane.getMaximumDividerLocation()){ + // Minimize editor pane + splitpane.getTopComponent().setMinimumSize(new Dimension()); + splitpane.getBottomComponent().setMinimumSize(null); + splitpane.setDividerLocation(0.); + splitpane.setResizeWeight(0.); + } + else if(splitpane.getDividerLocation()<=splitpane.getMinimumDividerLocation()){ + // Restore editor pane + splitpane.getTopComponent().setMinimumSize(null); + splitpane.getBottomComponent().setMinimumSize(null); + splitpane.setResizeWeight(0.); + // Could probably catch resize edge-cases etc in pce too + if(dividerLastPosition>=splitpane.getMaximumDividerLocation()||dividerLastPosition<=splitpane.getMinimumDividerLocation()) + dividerLastPosition=splitpane.getMaximumDividerLocation()/2; + splitpane.setDividerLocation(dividerLastPosition); + } + else{ + // Maximize editor pane + splitpane.getBottomComponent().setMinimumSize(new Dimension()); + splitpane.getTopComponent().setMinimumSize(null); + splitpane.setDividerLocation(splitpane.getOrientation()==VERTICAL_SPLIT?splitpane.getHeight()-splitpane.getDividerSize():splitpane.getWidth()-splitpane.getDividerSize()); + splitpane.setResizeWeight(1.); + } + } + + private void toggleDividerOrientation() { + if (splitpane.getOrientation() == JSplitPane.VERTICAL_SPLIT) + splitpane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); + else + splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT); + + splitpane.setDividerLocation(0.5); + } + + public StudioPanel(Server server,String filename) { + + registerForMacOSXEvents(); + + windowListChangedEventListener = new WindowListChangedEventListener() { + + public void WindowListChangedEventOccurred(WindowListChangedEvent evt) { + rebuildMenuBar(); + rebuildToolbar(); + } + }; + + windowListMonitor.addEventListener(windowListChangedEventListener); + + splitpane = new JSplitPane(); + frame = new JFrame(); + windowList.add(this); + + initDocument(); + setServer(server); + + menubar = createMenuBar(); + toolbar = createToolbar(); + + tabbedPane = new JTabbedPane(); + splitpane.setBottomComponent(tabbedPane); + splitpane.setOneTouchExpandable(true); + splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT); + try { + Component divider = ((BasicSplitPaneUI) splitpane.getUI()).getDivider(); + + divider.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent event) { + if (event.getClickCount() == 2) + toggleDividerOrientation(); + } + }); + } + catch (ClassCastException e) { + } + splitpane.setContinuousLayout(true); + + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + + frame.setJMenuBar(menubar); + + if (filename != null) + loadFile(filename); + else + myScriptNumber = scriptNumber++; + + refreshFrameTitle(); + + frame.getContentPane().add(toolbar,BorderLayout.NORTH); + frame.getContentPane().add(splitpane,BorderLayout.CENTER); + // frame.setSize(frame.getContentPane().getPreferredSize()); + + frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + frame.addWindowListener(this); + frame.setSize((int) (0.8 * screenSize.width), + (int) (0.8 * screenSize.height)); + + frame.setLocation(((int) Math.max(0,(screenSize.width - frame.getWidth()) / 2.0)), + (int) (Math.max(0,(screenSize.height - frame.getHeight()) / 2.0))); + + frame.setIconImage(getImage(Config.imageBase + "32x32/dot-chart.png").getImage()); + + // frame.pack(); + frame.setVisible(true); + splitpane.setDividerLocation(0.5); + + textArea.requestFocus(); + splitpane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY,new PropertyChangeListener(){ + public void propertyChange(PropertyChangeEvent pce){ + String s=splitpane.getDividerLocation()>=splitpane.getMaximumDividerLocation()?I18n.getString("MinimizeEditorPane"):splitpane.getDividerLocation()<=splitpane.getMinimumDividerLocation()?I18n.getString("RestoreEditorPane"):I18n.getString("MaximizeEditorPane"); + minMaxDividerAction.putValue(Action.SHORT_DESCRIPTION,s); + minMaxDividerAction.putValue(Action.NAME,s); + if(splitpane.getDividerLocation()splitpane.getMinimumDividerLocation()) + dividerLastPosition=splitpane.getDividerLocation(); + } + }); + dividerLastPosition=splitpane.getDividerLocation(); + } + + public void update(Observable obs,Object obj) { + } + public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x")); + private static boolean registeredForMaxOSXEvents = false; + + public void registerForMacOSXEvents() { + if (registeredForMaxOSXEvents) + return; + + if (MAC_OS_X) + try { + // Generate and register the OSXAdapter, passing it a hash of all the methods we wish to + // use as delegates for various com.apple.eawt.ApplicationListener methods + OSXAdapter.setQuitHandler(this, StudioPanel.class.getDeclaredMethod("quit",(Class[]) null)); + OSXAdapter.setAboutHandler(this, StudioPanel.class.getDeclaredMethod("about",(Class[]) null)); + OSXAdapter.setPreferencesHandler(this, StudioPanel.class.getDeclaredMethod("settings",(Class[]) null)); + registeredForMaxOSXEvents = true; + } + catch (Exception e) { + System.err.println("Error while loading the OSXAdapter:"); + e.printStackTrace(); + } + } + + public static void init(String[] args) { + try { + String filename = null; + + String[] mruFiles = Config.getInstance().getMRUFiles(); + if(args.length>0){ + File f=new File(args[0]); + if(f.exists()) + filename=args[0]; + } else if (mruFiles.length > 0) { + File f = new File(mruFiles[0]); + if (f.exists()) + filename = mruFiles[0]; + } + + Locale.setDefault(Locale.US); + + Server s = null; + String lruServer = Config.getInstance().getLRUServer(); + if (Config.getInstance().getServerNames().contains(lruServer)){ + s = Config.getInstance().getServer(lruServer); + } + new StudioPanel(s,filename); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + public void refreshQuery() { + table = null; + executeK4Query(lastQuery); + } + + public void executeQueryCurrentLine() { + executeQuery(getCurrentLineEditorText(textArea)); + } + + public void executeQuery() { + executeQuery(getEditorText(textArea)); + } + + private void executeQuery(String text) { + table = null; + + if (text == null) { + JOptionPane.showMessageDialog(frame, + "\nNo text available to submit to server.\n\n", + "Studio for kdb+", + JOptionPane.OK_OPTION, + getImage(Config.imageBase + "32x32/information.png")); + + return; + } + + refreshAction.setEnabled(false); + stopAction.setEnabled(true); + executeAction.setEnabled(false); + executeCurrentLineAction.setEnabled(false); + exportAction.setEnabled(false); + chartAction.setEnabled(false); + openInExcel.setEnabled(false); + + executeK4Query(text); + + lastQuery = text; + } + + private String getEditorText(JEditorPane editor) { + String text = editor.getSelectedText(); + + if (text != null) { + if (text.length() > 0) + if (text.trim().length() == 0) + return null; // selected text is whitespace + } + else + text = editor.getText(); // get the full text then + + if (text != null) + text = text.trim(); + + if (text.trim().length() == 0) + text = null; + + return text; + } + + private String getCurrentLineEditorText(JEditorPane editor) { + String newLine = "\n"; + String text = null; + + try { + int pos = editor.getCaretPosition(); + int max = editor.getDocument().getLength(); + + + if ((max > pos) && (!editor.getText(pos,1).equals("\n"))) { + String toeol = editor.getText(pos,max - pos); + int eol = toeol.indexOf('\n'); + + if (eol > 0) + pos = pos + eol; + else + pos = max; + } + + text = editor.getText(0,pos); + + int lrPos = text.lastIndexOf(newLine); + + if (lrPos >= 0) { + lrPos += newLine.length(); // found it so skip it + text = text.substring(lrPos,pos).trim(); + } + } + catch (BadLocationException e) { + } + + if (text != null) { + text = text.trim(); + + if (text.length() == 0) + text = null; + } + + return text; + } + + private void processK4Results(K.KBase r) throws c.K4Exception { + if (r != null) { + exportAction.setEnabled(true); + KTableModel model = KTableModel.getModel(r); + if (model != null) { + boolean dictModel = model instanceof DictModel; + boolean listModel = model instanceof ListModel; + boolean tableModel = ! (dictModel || listModel); + QGrid grid = new QGrid(model); + table = grid.getTable(); + openInExcel.setEnabled(true); + chartAction.setEnabled(tableModel); + String title = tableModel ? "Table" : (dictModel ? "Dict" : "List"); + TabPanel frame = new TabPanel( title + " [" + grid.getRowCount() + " rows] ", + getImage(Config.imageBase2 + "table.png"), + grid); +// frame.setTitle(I18n.getString("Table")+" [" + grid.getRowCount() + " "+I18n.getString("rows")+"] "); + tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); + } else { + chartAction.setEnabled(false); + openInExcel.setEnabled(false); + LimitedWriter lm = new LimitedWriter(50000); + try { + if(!(r instanceof K.UnaryPrimitive&&0==((K.UnaryPrimitive)r).getPrimitiveAsInt())) + r.toString(lm,true); + } + catch (IOException ex) { + ex.printStackTrace(); + } + catch (LimitedWriter.LimitException ex) { + } + + JEditorPane pane = new JEditorPane("text/plain",lm.toString()); + pane.setFont(font); + +//pane.setLineWrap( false); +//pane.setWrapStyleWord( false); + + JScrollPane scrollpane = new JScrollPane(pane, + ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + TabPanel frame = new TabPanel("Console View ", + getImage(Config.imageBase2 + "console.png"), + scrollpane); + + frame.setTitle(I18n.getString("ConsoleView")); + + tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); + } + } + else { + // Log that execute was successful + } + } + Server server = null; + + public void executeK4Query(final String text) { + final Cursor cursor = textArea.getCursor(); + + textArea.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)); + tabbedPane.removeAll(); + worker = new SwingWorker() { + Server s = null; + c c = null; + K.KBase r = null; + Throwable exception; + boolean cancelled = false; + long execTime=0; + public void interrupt() { + super.interrupt(); + + cancelled = true; + + if (c != null) + c.close(); + cleanup(); + } + + public Object construct() { + try { + this.s = server; + c = ConnectionPool.getInstance().leaseConnection(s); + c.setFrame(frame); + long startTime=System.currentTimeMillis(); + c.k(new K.KCharacterVector(text)); + r = c.getResponse(); + execTime=System.currentTimeMillis()-startTime; + } + catch (Throwable e) { + System.err.println("Error occurred during query execution: " + e); + e.printStackTrace(System.err); + exception = e; + } + + return null; + } + + public void finished() { + if (!cancelled) { + if (exception != null) + try { + throw exception; + } + catch (IOException ex) { + JOptionPane.showMessageDialog(frame, + "\nA communications error occurred whilst sending the query.\n\nPlease check that the server is running on " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + ex.getMessage() + "\n\n", + "Studio for kdb+", + JOptionPane.ERROR_MESSAGE, + getImage(Config.imageBase + "32x32/error.png")); + } + catch (c.K4Exception ex) { + JTextPane pane = new JTextPane(); + String hint = QErrors.lookup(ex.getMessage()); + if (hint != null) + hint = "\nStudio Hint: Possibly this error refers to " + hint; + else + hint = ""; + pane.setText("An error occurred during execution of the query.\nThe server sent the response:\n" + ex.getMessage() + hint); + pane.setForeground(Color.RED); + + JScrollPane scrollpane = new JScrollPane(pane); + + TabPanel frame = new TabPanel("Error Details ", + getImage(Config.imageBase2 + "error.png"), + scrollpane); + frame.setTitle("Error Details "); + + tabbedPane.addTab(frame.getTitle(),frame.getIcon(),frame.getComponent()); + + // tabbedPane.setSelectedComponent(resultsTabbedPane); + } + catch (java.lang.OutOfMemoryError ex) { + JOptionPane.showMessageDialog(frame, + "\nOut of memory whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nThe result set is probably too large.\n\nTry increasing the memory available to studio through the command line option -J -Xmx512m\n\n", + "Studio for kdb+", + JOptionPane.ERROR_MESSAGE, + getImage(Config.imageBase + "32x32/error.png")); + } + catch (Throwable ex) { + String message = ex.getMessage(); + + if ((message == null) || (message.length() == 0)) + message = "No message with exception. Exception is " + ex.toString(); + + JOptionPane.showMessageDialog(frame, + "\nAn unexpected error occurred whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + message + "\n\n", + "Studio for kdb+", + JOptionPane.ERROR_MESSAGE, + getImage(Config.imageBase + "32x32/error.png")); + } + else + try { + Utilities.setStatusText(textArea, "Last execution time:"+(execTime>0?""+execTime:"<1")+" mS"); + processK4Results(r); + } + catch (Exception e) { + e.printStackTrace(System.err); + JOptionPane.showMessageDialog(frame, + "\nAn unexpected error occurred whilst communicating with " + server.getHost() + ":" + server.getPort() + "\n\nError detail is\n\n" + e.getMessage() + "\n\n", + "Studio for kdb+", + JOptionPane.ERROR_MESSAGE, + getImage(Config.imageBase + "32x32/error.png")); + } + + cleanup(); + } + } + + private void cleanup() { + if (c != null) + ConnectionPool.getInstance().freeConnection(s,c); + //if( c != null) + // c.close(); + c = null; + + textArea.setCursor(cursor); + + stopAction.setEnabled(false); + executeAction.setEnabled(true); + executeCurrentLineAction.setEnabled(true); + refreshAction.setEnabled(true); + + System.gc(); + + worker = null; + } + }; + + worker.start(); + } + private SwingWorker worker; + + public void windowClosing(WindowEvent e) { + if (quitWindow()) + if (windowList.size() == 0) + System.exit(0); + } + + + public void windowClosed(WindowEvent e) { + } + + + public void windowOpened(WindowEvent e) { + } + // ctrl-alt spacebar to minimize window + + public void windowIconified(WindowEvent e) { + } + + + public void windowDeiconified(WindowEvent e) { + } + + + public void windowActivated(WindowEvent e) { + this.invalidate(); + SwingUtilities.updateComponentTreeUI(this); + } + + + public void windowDeactivated(WindowEvent e) { + } + + private class MarkingDocumentListener implements DocumentListener { + private boolean modified = false; + + private void setModified(boolean b) { + modified = b; + } + + private boolean getModified() { + return modified; + } + private Component comp; + + public MarkingDocumentListener(Component comp) { + this.comp = comp; + } + + private void markChanged(DocumentEvent evt) { + setModified(true); + refreshFrameTitle(); + } + + + public void changedUpdate(DocumentEvent e) { + } + + + public void insertUpdate(DocumentEvent evt) { + markChanged(evt); + } + + + public void removeUpdate(DocumentEvent evt) { + markChanged(evt); + } + /** Document property holding String name of associated file */ + private static final String FILE = "file"; + /** Document property holding Boolean if document was created or opened */ + private static final String CREATED = "created"; + /** Document property holding Boolean modified information */ + private static final String MODIFIED = "modified"; + } + + public static ImageIcon getImage(String strFilename) { + Class thisClass = StudioPanel.class; + + java.net.URL url = null; + + if (strFilename.startsWith("/")) + url = thisClass.getResource(strFilename); + else + // Locate the desired image file and create a URL to it + url = thisClass.getResource("/toolbarButtonGraphics/" + strFilename); + + // See if we successfully found the image + if (url == null) + //System.out.println("Unable to load the following image: " + + // strFilename); + return null; + + Toolkit toolkit = Toolkit.getDefaultToolkit(); + Image image = toolkit.getImage(url); + return new ImageIcon(image); + } +} From c1e97a943c9bf8d70b711c980aa83f49cfdc4163 Mon Sep 17 00:00:00 2001 From: sylvain Date: Sat, 27 Jun 2020 15:40:30 -0400 Subject: [PATCH 3/5] K.java - Add toString on BinaryPrimitive --- src/studio/kdb/K.java | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index d6031261..f0b38056 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -12,8 +12,8 @@ import java.util.function.ToLongFunction; public class K { - private static SimpleDateFormat formatter = new SimpleDateFormat(); - private static DecimalFormat nsFormatter = new DecimalFormat("000000000"); + private static final SimpleDateFormat formatter = new SimpleDateFormat(); + private static final DecimalFormat nsFormatter = new DecimalFormat("000000000"); static { formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); @@ -79,7 +79,7 @@ public void setAttr(byte attr) { this.attr = attr; } - private static String[] sAttr = new String[]{"", "`s#", "`u#", "`p#", "`g#"}; + private static final String[] sAttr = new String[]{"", "`s#", "`u#", "`p#", "`g#"}; public String toString(boolean showType) { if (attr <= sAttr.length) @@ -122,6 +122,11 @@ public BinaryPrimitive(int i) { type = 102; } + @Override + public String toString(boolean showType) { + return getPrimitive(); + } + public void toString(LimitedWriter w, boolean showType) throws IOException { w.write(getPrimitive()); } @@ -222,7 +227,7 @@ public String getDataType() { return "Function"; } - private String body; + private final String body; public Function(KCharacterVector body) { type = 100; @@ -247,13 +252,12 @@ public String getDataType() { return "Primitive"; } - private int primitive; - private String s = " "; + private final int primitive; + private final String s; public Primitive(String[] ops, int i) { primitive = i; - if (i >= 0 && i < ops.length) - s = ops[i]; + s = (i >= 0 && i < ops.length) ? ops[i] : " "; } public String getPrimitive() { @@ -270,7 +274,7 @@ public String getDataType() { return "Projection"; } - private K.KList objs; + private final K.KList objs; public Projection(K.KList objs) { type = 104; @@ -336,7 +340,7 @@ public static void init(char[] ops, int[] values) { map.put(values[i], ops[i]); } - private int primitive; + private final int primitive; private char charVal = ' '; @@ -366,7 +370,7 @@ public void toString(LimitedWriter w, boolean showType) throws IOException { } public static class UnaryPrimitive extends Primitive { - private static String[] ops = {"::", "+:", "-:", "*:", "%:", "&:", "|:", "^:", "=:", "<:", ">:", "$:", ",:", "#:", "_:", "~:", "!:", "?:", "@:", ".:", "0::", "1::", "2::", "avg", "last", "sum", "prd", "min", "max", "exit", "getenv", "abs", "sqrt", "log", "exp", "sin", "asin", "cos", "acos", "tan", "atan", "enlist", "var", "dev", "hopen"}; + private static final String[] ops = {"::", "+:", "-:", "*:", "%:", "&:", "|:", "^:", "=:", "<:", ">:", "$:", ",:", "#:", "_:", "~:", "!:", "?:", "@:", ".:", "0::", "1::", "2::", "avg", "last", "sum", "prd", "min", "max", "exit", "getenv", "abs", "sqrt", "log", "exp", "sin", "asin", "cos", "acos", "tan", "atan", "enlist", "var", "dev", "hopen"}; public UnaryPrimitive(int i) { super(ops, i); @@ -1061,7 +1065,7 @@ else if (i == -Integer.MAX_VALUE) public Date toDate() { int m = i + 24000, y = m / 12; Calendar cal = Calendar.getInstance(); - cal.set(y, m, 01); + cal.set(y, m, 1); return cal.getTime(); } From 61b123577d8252b93c41bee304f6a9b005b96f83 Mon Sep 17 00:00:00 2001 From: sylvain Date: Sat, 27 Jun 2020 15:45:02 -0400 Subject: [PATCH 4/5] K.java - Move toString to Primitive --- src/studio/kdb/K.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index f0b38056..a318dc2c 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -122,11 +122,6 @@ public BinaryPrimitive(int i) { type = 102; } - @Override - public String toString(boolean showType) { - return getPrimitive(); - } - public void toString(LimitedWriter w, boolean showType) throws IOException { w.write(getPrimitive()); } @@ -260,6 +255,12 @@ public Primitive(String[] ops, int i) { s = (i >= 0 && i < ops.length) ? ops[i] : " "; } + + @Override + public String toString(boolean showType) { + return getPrimitive(); + } + public String getPrimitive() { return s; } From 9ccf5d066d8e7c16782b6cc78a05e6e131b1df61 Mon Sep 17 00:00:00 2001 From: sylvain Date: Wed, 15 Jul 2020 21:59:26 -0400 Subject: [PATCH 5/5] Fix toString for copy/paste for all KBaseVector classes --- src/studio/kdb/K.java | 325 ++++++++++++++++++++++++++++-------------- 1 file changed, 216 insertions(+), 109 deletions(-) diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index 33e51e66..8de54179 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -58,11 +58,6 @@ public void serialise(OutputStream o) throws IOException { } - /* public String toString(boolean showType) { - return ""; - } - ; - */ public String toString() { return toString(true); } @@ -97,6 +92,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Adverb extends KBase { + @Override public String getDataType() { return "Adverb"; } @@ -115,6 +111,7 @@ public Object getObject() { public static class BinaryPrimitive extends Primitive { private static final String[] ops = {":", "+", "-", "*", "%", "&", "|", "^", "=", "<", ">", "$", ",", "#", "_", "~", "!", "?", "@", ".", "0:", "1:", "2:", "in", "within", "like", "bin", "ss", "insert", "wsum", "wavg", "div", "xexp", "setenv", "binr", "cov", "cor"}; + @Override public String getDataType() { return "Binary Primitive"; } @@ -124,6 +121,7 @@ public BinaryPrimitive(int i) { type = 102; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(getPrimitive()); } @@ -132,6 +130,7 @@ public void toString(Writer w, boolean showType) throws IOException { public static class FComposition extends KBase { Object[] objs; + @Override public String getDataType() { return "Function Composition"; } @@ -152,6 +151,7 @@ public FEachLeft(K.KBase o) { type = 111; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("\\:"); @@ -164,6 +164,7 @@ public FEachRight(K.KBase o) { type = 110; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("/:"); @@ -176,6 +177,7 @@ public FPrior(K.KBase o) { type = 109; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("':"); @@ -188,6 +190,7 @@ public Feach(K.KBase o) { type = 106; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("'"); @@ -200,6 +203,7 @@ public Fover(K.KBase o) { type = 107; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("/"); @@ -213,6 +217,7 @@ public Fscan(KBase o) { this.o = o; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("\\"); @@ -220,6 +225,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Function extends KBase { + @Override public String getDataType() { return "Function"; } @@ -235,16 +241,19 @@ public String getBody() { return body; } + @Override public String toString(boolean showType) { return body; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(body); } } public static class Primitive extends KBase { + @Override public String getDataType() { return "Primitive"; } @@ -254,7 +263,7 @@ public String getDataType() { public Primitive(String[] ops, int i) { primitive = i; - s = (i >= 0 && i < ops.length) ? ops[i] : " "; + s = (i >= 0 && i < ops.length) ? ops[i] : " "; } @@ -273,6 +282,7 @@ public int getPrimitiveAsInt() { } public static class Projection extends KBase { + @Override public String getDataType() { return "Projection"; } @@ -284,6 +294,7 @@ public Projection(K.KList objs) { this.objs = objs; } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean listProjection = false; if ((objs.getLength() > 0) && (objs.at(0) instanceof UnaryPrimitive)) { @@ -332,6 +343,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class TernaryOperator extends KBase { + @Override public String getDataType() { return "Ternary Operator"; } @@ -367,6 +379,7 @@ public int getPrimitiveAsInt() { return primitive; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(charVal); } @@ -380,6 +393,7 @@ public UnaryPrimitive(int i) { type = 101; } + @Override public void toString(Writer w, boolean showType) throws IOException { if (getPrimitiveAsInt() == -1) return; @@ -388,6 +402,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Variable extends KBase { + @Override public String getDataType() { return "Variable"; } @@ -421,6 +436,7 @@ public void setType(short type) { } public static class KBoolean extends KBase implements ToDouble { + @Override public String getDataType() { return "Boolean"; } @@ -432,6 +448,7 @@ public KBoolean(boolean b) { type = -1; } + @Override public String toString(boolean showType) { String s = b ? "1" : "0"; if (showType) @@ -439,10 +456,12 @@ public String toString(boolean showType) { return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public double toDouble() { return b ? 1.0 : 0.0; } @@ -453,12 +472,14 @@ public boolean toBoolean() { } public static class KByte extends KBase implements ToDouble { + @Override public String getDataType() { return "Byte"; } public byte b; + @Override public double toDouble() { return b; } @@ -468,22 +489,26 @@ public KByte(byte b) { type = -4; } + @Override public String toString(boolean showType) { return "0x" + Integer.toHexString((b >> 4) & 0xf) + Integer.toHexString(b & 0xf); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KShort extends KBase implements ToDouble { + @Override public String getDataType() { return "Short"; } public short s; + @Override public double toDouble() { return s; } @@ -493,10 +518,12 @@ public KShort(short s) { type = -5; } + @Override public boolean isNull() { return s == Short.MIN_VALUE; } + @Override public String toString(boolean showType) { String t; if (s == Short.MIN_VALUE) @@ -512,18 +539,21 @@ else if (s == -Short.MAX_VALUE) return t; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KInteger extends KBase implements ToDouble { + @Override public String getDataType() { return "Integer"; } public int i; + @Override public double toDouble() { return i; } @@ -533,10 +563,12 @@ public KInteger(int i) { type = -6; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { String s; if (isNull()) @@ -552,12 +584,14 @@ else if (i == -Integer.MAX_VALUE) return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KSymbol extends KBase { + @Override public String getDataType() { return "Symbol"; } @@ -569,32 +603,38 @@ public KSymbol(String s) { type = -11; } + @Override public String toString(boolean showType) { return s; } + @Override public boolean isNull() { return s.length() == 0; } + @Override public void toString(Writer w, boolean showType) throws IOException { if (showType) w.write("`"); w.write(s); } + @Override public void serialise(OutputStream o) throws IOException { o.write(s.getBytes(Config.getInstance().getEncoding())); } } public static class KLong extends KBase implements ToDouble, ToLongFunction { + @Override public String getDataType() { return "Long"; } public long j; + @Override public double toDouble() { return j; } @@ -609,10 +649,12 @@ public KLong(long j) { type = -7; } + @Override public boolean isNull() { return j == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { String s; if (isNull()) @@ -630,10 +672,12 @@ else if (j == -Long.MAX_VALUE) return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, j); @@ -641,6 +685,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KCharacter extends KBase { + @Override public String getDataType() { return "Character"; } @@ -652,10 +697,12 @@ public KCharacter(char c) { type = -10; } + @Override public boolean isNull() { return c == ' '; } + @Override public String toString(boolean showType) { if (showType) return "\"" + c + "\""; @@ -663,10 +710,12 @@ public String toString(boolean showType) { return "" + c; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) c); @@ -674,12 +723,14 @@ public void serialise(OutputStream o) throws IOException { } public static class KFloat extends KBase implements ToDouble { + @Override public String getDataType() { return "Float"; } public float f; + @Override public double toDouble() { return f; } @@ -689,10 +740,12 @@ public KFloat(float f) { this.f = f; } + @Override public boolean isNull() { return Float.isNaN(f); } + @Override public String toString(boolean showType) { if (isNull()) return "0ne"; @@ -712,10 +765,12 @@ else if (f == Float.NEGATIVE_INFINITY) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); int i = Float.floatToIntBits(f); @@ -724,6 +779,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KDouble extends KBase implements ToDouble { + @Override public String getDataType() { return "Double"; } @@ -735,14 +791,17 @@ public KDouble(double d) { this.d = d; } + @Override public double toDouble() { return d; } + @Override public boolean isNull() { return Double.isNaN(d); } + @Override public String toString(boolean showType) { if (isNull()) return "0n"; @@ -762,10 +821,12 @@ else if (d == Double.NEGATIVE_INFINITY) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); long j = Double.doubleToLongBits(d); @@ -774,6 +835,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KDate extends KBase { + @Override public String getDataType() { return "Date"; } @@ -785,10 +847,12 @@ public KDate(int date) { this.date = date; } + @Override public boolean isNull() { return date == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nd"; @@ -800,6 +864,7 @@ else if (date == -Integer.MAX_VALUE) return sd("yyyy.MM.dd", new Date(86400000L * (date + 10957))); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -812,6 +877,7 @@ public Date toDate() { public static class KGuid extends KBase { static UUID nuuid = new UUID(0, 0); + @Override public String getDataType() { return "Guid"; } @@ -823,20 +889,24 @@ public KGuid(UUID uuid) { this.uuid = uuid; } + @Override public boolean isNull() { return uuid == nuuid; } + @Override public String toString(boolean showType) { return uuid.toString(); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KTime extends KBase { + @Override public String getDataType() { return "Time"; } @@ -848,10 +918,12 @@ public KTime(int time) { this.time = time; } + @Override public boolean isNull() { return time == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nt"; @@ -863,6 +935,7 @@ else if (time == -Integer.MAX_VALUE) return sd("HH:mm:ss.SSS", new Time(time)); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -873,6 +946,7 @@ public Time toTime() { } public static class KDatetime extends KBase { + @Override public String getDataType() { return "Datetime"; } @@ -884,10 +958,12 @@ public KDatetime(double time) { this.time = time; } + @Override public boolean isNull() { return Double.isNaN(time); } + @Override public String toString(boolean showType) { if (isNull()) return "0nz"; @@ -899,6 +975,7 @@ else if (time == Double.NEGATIVE_INFINITY) return sd("yyyy.MM.dd HH:mm:ss.SSS", toTimestamp()); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -910,6 +987,7 @@ public Timestamp toTimestamp() { public static class KTimestamp extends KBase { + @Override public String getDataType() { return "Timestamp"; } @@ -921,10 +999,12 @@ public KTimestamp(long time) { this.time = time; } + @Override public boolean isNull() { return time == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Np"; @@ -938,6 +1018,7 @@ else if (time == -Long.MAX_VALUE) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -955,6 +1036,7 @@ public Timestamp toTimestamp() { } public static class Dict extends KBase { + @Override public String getDataType() { return "Dictionary"; } @@ -983,6 +1065,7 @@ public void upsert(K.Dict upd) { cy.append(updy); } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean useBrackets = getAttr() != 0 || x instanceof Flip; w.write(super.toString(showType)); @@ -1002,6 +1085,7 @@ public String toString(boolean showType) { } public static class Flip extends KBase { + @Override public String getDataType() { return "Flip"; } @@ -1015,6 +1099,7 @@ public Flip(Dict X) { y = (K.KBaseVector) X.y; } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean usebracket = x.getLength() == 1; w.write(flip); @@ -1034,6 +1119,7 @@ public void append(Flip nf) { } public static class Month extends KBase { + @Override public String getDataType() { return "Month"; } @@ -1045,10 +1131,12 @@ public Month(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nm"; @@ -1072,12 +1160,14 @@ public Date toDate() { return cal.getTime(); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class Minute extends KBase { + @Override public String getDataType() { return "Minute"; } @@ -1089,10 +1179,12 @@ public Minute(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nu"; @@ -1104,6 +1196,7 @@ else if (i == -Integer.MAX_VALUE) return i2(i / 60) + ":" + i2(i % 60); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1117,6 +1210,7 @@ public Date toDate() { } public static class Second extends KBase { + @Override public String getDataType() { return "Second"; } @@ -1128,10 +1222,12 @@ public Second(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nv"; @@ -1143,6 +1239,7 @@ else if (i == -Integer.MAX_VALUE) return new Minute(i / 60).toString() + ':' + i2(i % 60); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1164,14 +1261,17 @@ public KTimespan(long x) { type = -16; } + @Override public String getDataType() { return "Timespan"; } + @Override public boolean isNull() { return j == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nn"; @@ -1196,6 +1296,7 @@ else if (j == -Long.MAX_VALUE) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1243,6 +1344,27 @@ protected int calcCapacity(int length) { return (int) (1.1 * length); } + @Override + public final String toString(boolean showType) { + try { + CharArrayWriter writer = new CharArrayWriter(); + writer.write(super.toString(showType)); + toString(writer, showType); + return writer.toString(); + } catch (IOException e) { + e.printStackTrace(); + return super.toString(showType); + } + } + + @Override + public final void toString(Writer w, boolean showType) throws IOException { + w.write(super.toString(showType)); + toStringVector(w, showType); + } + + abstract void toStringVector(Writer w, boolean showType) throws IOException; + public void append(KBaseVector x) { if ((x.getLength() + getLength()) > Array.getLength(getArray())) { int newLength = Array.getLength(getArray()) + x.getLength(); @@ -1256,6 +1378,7 @@ public void append(KBaseVector x) { } public static class KShortVector extends KBaseVector { + @Override public String getDataType() { return "Short Vector"; } @@ -1265,13 +1388,13 @@ public KShortVector(int length) { type = 5; } + @Override public KBase at(int i) { return new KShort(Array.getShort(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`short$()"); else { @@ -1298,6 +1421,7 @@ else if (v == -Short.MAX_VALUE) } public static class KIntVector extends KBaseVector { + @Override public String getDataType() { return "Int Vector"; } @@ -1307,13 +1431,13 @@ public KIntVector(int length) { type = 6; } + @Override public KBase at(int i) { return new KInteger(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`int$()"); else { @@ -1339,6 +1463,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KList extends KBaseVector { + @Override public String getDataType() { return "List"; } @@ -1348,13 +1473,13 @@ public KList(int length) { type = 0; } + @Override public KBase at(int i) { return (KBase) Array.get(array, i); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 1) w.write(enlist); else @@ -1367,28 +1492,10 @@ public void toString(Writer w, boolean showType) throws IOException { if (getLength() != 1) w.write(")"); } - - @Override - public String toString(boolean showType) { - StringBuilder sb = new StringBuilder(); - sb.append(super.toString(showType)); - if (getLength() == 1) { - sb.append(enlist); - } else { - sb.append("("); - } - for (int i = 0; i < getLength(); i++) { - if (i > 0) - sb.append(";"); - sb.append(at(i).toString(showType)); - } - if (getLength() != 1) - sb.append(")"); - return sb.toString(); - } } public static class KDoubleVector extends KBaseVector { + @Override public String getDataType() { return "Double Vector"; } @@ -1398,13 +1505,13 @@ public KDoubleVector(int length) { type = 9; } + @Override public KBase at(int i) { return new KDouble(Array.getDouble(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`float$()"); else { @@ -1441,6 +1548,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KFloatVector extends KBaseVector { + @Override public String getDataType() { return "Float Vector"; } @@ -1450,13 +1558,13 @@ public KFloatVector(int length) { type = 8; } + @Override public KBase at(int i) { return new KFloat(Array.getFloat(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`real$()"); else { @@ -1491,6 +1599,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KLongVector extends KBaseVector { + @Override public String getDataType() { return "Long Vector"; } @@ -1500,13 +1609,13 @@ public KLongVector(int length) { type = 7; } + @Override public KBase at(int i) { return new KLong(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`long$()"); else { @@ -1533,6 +1642,7 @@ else if (v == -Long.MAX_VALUE) } public static class KMonthVector extends KBaseVector { + @Override public String getDataType() { return "Month Vector"; } @@ -1542,13 +1652,13 @@ public KMonthVector(int length) { type = 13; } + @Override public KBase at(int i) { return new Month(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`month$()"); else { @@ -1577,6 +1687,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KDateVector extends KBaseVector { + @Override public String getDataType() { return "Date Vector"; } @@ -1586,13 +1697,13 @@ public KDateVector(int length) { type = 14; } + @Override public KBase at(int i) { return new KDate(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`date$()"); else { @@ -1621,6 +1732,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KGuidVector extends KBaseVector { + @Override public String getDataType() { return "Guid Vector"; } @@ -1630,13 +1742,13 @@ public KGuidVector(int length) { type = 2; } + @Override public KBase at(int i) { return new KGuid((UUID) Array.get(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`guid$()"); else { @@ -1652,6 +1764,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KMinuteVector extends KBaseVector { + @Override public String getDataType() { return "Minute Vector"; } @@ -1661,13 +1774,13 @@ public KMinuteVector(int length) { type = 17; } + @Override public KBase at(int i) { return new Minute(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`minute$()"); else { @@ -1691,6 +1804,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KDatetimeVector extends KBaseVector { + @Override public String getDataType() { return "Datetime Vector"; } @@ -1700,13 +1814,13 @@ public KDatetimeVector(int length) { type = 15; } + @Override public KBase at(int i) { return new KDatetime(Array.getDouble(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`datetime$()"); else { @@ -1737,6 +1851,7 @@ else if (d == Double.NEGATIVE_INFINITY) } public static class KTimestampVector extends KBaseVector { + @Override public String getDataType() { return "Timestamp Vector"; } @@ -1746,13 +1861,13 @@ public KTimestampVector(int length) { type = 12; } + @Override public KBase at(int i) { return new KTimestamp(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`timestamp$()"); else { @@ -1768,6 +1883,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KTimespanVector extends KBaseVector { + @Override public String getDataType() { return "Timespan Vector"; } @@ -1777,13 +1893,13 @@ public KTimespanVector(int length) { type = 16; } + @Override public KBase at(int i) { return new KTimespan(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`timespan$()"); else { @@ -1799,6 +1915,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KSecondVector extends KBaseVector { + @Override public String getDataType() { return "Second Vector"; } @@ -1808,10 +1925,12 @@ public KSecondVector(int length) { type = 18; } + @Override public KBase at(int i) { return new Second(Array.getInt(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1820,8 +1939,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`second$()"); @@ -1846,6 +1965,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KTimeVector extends KBaseVector { + @Override public String getDataType() { return "Time Vector"; } @@ -1855,10 +1975,12 @@ public KTimeVector(int length) { type = 19; } + @Override public KBase at(int i) { return new KTime(Array.getInt(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1867,9 +1989,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`time$()"); else { @@ -1893,6 +2014,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KBooleanVector extends KBaseVector { + @Override public String getDataType() { return "Boolean Vector"; } @@ -1902,10 +2024,12 @@ public KBooleanVector(int length) { type = 1; } + @Override public KBase at(int i) { return new KBoolean(Array.getBoolean(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1914,8 +2038,8 @@ public void serialise(OutputStream o) throws IOException { write(o, (byte) (Array.getBoolean(array, i) ? 1 : 0)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`boolean$()"); else { @@ -1929,6 +2053,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KByteVector extends KBaseVector { + @Override public String getDataType() { return "Byte Vector"; } @@ -1938,10 +2063,12 @@ public KByteVector(int length) { type = 4; } + @Override public KBase at(int i) { return new KByte(Array.getByte(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1950,8 +2077,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getByte(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`byte$()"); else { @@ -1968,6 +2095,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KSymbolVector extends KBaseVector { + @Override public String getDataType() { return "Symbol Vector"; } @@ -1977,42 +2105,29 @@ public KSymbolVector(int length) { type = 11; } + @Override public KBase at(int i) { return new KSymbol((String) Array.get(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - toString(w); - } - private void toString(Appendable appendable) throws IOException { + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) { - appendable.append("0#`"); - return; - } - - if (getLength() == 1) { - appendable.append(enlist); - } - - for (int i = 0; i < getLength(); i++) - appendable.append("`").append(Array.get(array, i).toString()); - } + w.write("`symbol$()"); + } else { + if (getLength() == 1) { + w.write(enlist); + } - @Override - public String toString(boolean showType) { - try { - StringBuilder sb = new StringBuilder(); - toString(sb); - return sb.toString(); - } catch (IOException e) { - return super.toString(showType); + for (int i = 0; i < getLength(); i++) { + w.append("`").append(Array.get(array, i).toString()); + } } } } public static class KCharacterVector extends KBaseVector { + @Override public String getDataType() { return "Character Vector"; } @@ -2035,10 +2150,12 @@ public KCharacterVector(String s) { type = 10; } + @Override public KBase at(int i) { return new KCharacter(Array.getChar(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); byte[] b = new String((char[]) array).getBytes(Config.getInstance().getEncoding()); @@ -2047,8 +2164,8 @@ public void serialise(OutputStream o) throws IOException { o.write(b); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 1) w.write(enlist); @@ -2059,16 +2176,6 @@ public void toString(Writer w, boolean showType) throws IOException { if (showType) w.write("\""); } - - public String toString(boolean showType) { - CharArrayWriter w = new CharArrayWriter(); - try { - toString(w, showType); - } catch (IOException e) { - e.printStackTrace(System.err); - } - return w.toString(); - } } public static String decode(KBase obj, boolean showType) {