annotate src/samer/core_/shells/SwingShell.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * AppShell.java
samer@0 3 *
samer@0 4 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@0 12 package samer.core.shells;
samer@0 13
samer@0 14 import java.awt.*;
samer@0 15 import java.awt.event.*;
samer@0 16 import java.io.*;
samer@0 17 import java.util.*;
samer@0 18 import javax.swing.*;
samer@0 19
samer@0 20 import samer.core.*;
samer@0 21 import samer.core.util.*;
samer@0 22 import samer.core.util.shell.*;
samer@0 23 import samer.core.util.swing.*;
samer@0 24 import samer.core.util.swing.Frame;
samer@0 25 import samer.core.util.swing.Console;
samer@0 26 import samer.core.util.swing.Dialog;
samer@0 27 import samer.core.NumberViewer;
samer@0 28 import samer.core.viewers.swing.*;
samer@0 29 import samer.core.types.*;
samer@0 30
samer@0 31
samer@0 32 public class SwingShell extends AppShellBase
samer@0 33 {
samer@0 34 public SwingShell() { this(getDefaultPropertiesFile()); }
samer@0 35 public SwingShell(String props)
samer@0 36 {
samer@0 37 super(props);
samer@0 38 Shell.trace("SwingShell constructor");
samer@0 39 Shell.setShell(this);
samer@0 40 Shell.registerAgent(this);
samer@0 41
samer@0 42 // Shell.trace("creating console");
samer@0 43 // con = new Console();
samer@0 44 // JScrollPane scroller = new JScrollPane(con);
samer@0 45 // JComponent jc = (JComponent)(confr.getContentPane());
samer@0 46 // jc.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
samer@0 47 // Shell.registerAgent(con);
samer@0 48
samer@0 49 // confr.container().setLayout( new BorderLayout(4,5));
samer@0 50 // confr.container().add(scroller);
samer@0 51 // confr.container().add( new CommandField(20), "South");
samer@0 52 // confr.expose();
samer@0 53
samer@0 54 javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(false);
samer@0 55
samer@0 56
samer@0 57 Shell.trace("registering viewer classes");
samer@0 58 mput("VDouble","DoubleViewer");
samer@0 59 mput("VInteger","IntegerViewer");
samer@0 60 mput("VBoolean","BooleanViewer");
samer@0 61 mput("VString","StringViewer");
samer@0 62 mput("VParameter","ParameterViewer");
samer@0 63 mput("VFile","FileViewer");
samer@0 64 mput("VColor","ColorButton");
samer@0 65 put(Variable.class,StringViewer.class);
samer@0 66 put(Viewable.class,DefaultViewer.class);
samer@0 67
samer@0 68 try { execute("run",Shell.env()); }
samer@0 69 catch (Exception ex) { Shell.trace(ex.toString()); }
samer@0 70 }
samer@0 71
samer@0 72 private static void mput(String cl, String vwr) {
samer@0 73 samer.core.Registry.put("samer.core.types."+cl, "samer.core.viewers.swing."+vwr);
samer@0 74 }
samer@0 75
samer@0 76 class VContainer extends VContainerBase implements ViewableManager.ViewerContainer
samer@0 77 {
samer@0 78 VContainer() { super("exposed"); box.add(buttonBar); }
samer@0 79
samer@0 80 public void add(java.util.Iterator components)
samer@0 81 {
samer@0 82 adding=true;
samer@0 83 box.remove(glue); box.remove(buttonBar);
samer@0 84 while (components.hasNext()) {
samer@0 85 box.add((Component)(components.next()));
samer@0 86 }
samer@0 87 box.add(glue); box.add(buttonBar);
samer@0 88 box.validate();
samer@0 89 if (noScrollBars() || frame.getBounds().isEmpty()) frame.pack();
samer@0 90 else frame.validate();
samer@0 91 adding=false;
samer@0 92 }
samer@0 93
samer@0 94 public void windowClosing(WindowEvent e) {
samer@0 95 vm.releaseViewerContainer();
samer@0 96 super.windowClosing(e);
samer@0 97 }
samer@0 98 }
samer@0 99
samer@0 100 public ViewableManager.ViewerContainer getViewerContainer() {
samer@0 101 if (buttonBar==null) {
samer@0 102 buttonBar = new ButtonBar();
samer@0 103 buttonBar.setBroadcaster(am.getBroadcaster());
samer@0 104 buttonBar.setTarget(this);
samer@0 105 buttonBar.add("set").add("get").add("load").add("save").add("exit");
samer@0 106 }
samer@0 107 return new VContainer();
samer@0 108 }
samer@0 109
samer@0 110
samer@0 111 // ............. Factory methods ............
samer@0 112
samer@0 113 public Shell.Window getWindow(String title) { return new Frame(title); }
samer@0 114 public Shell.Dialog getDialog(String title) {
samer@0 115 // if (currentFrame==null) currentFrame=new JFrame("dialog owner");
samer@0 116 // return new Dialog(currentFrame,title,true);
samer@0 117 return new Dialog(title,true);
samer@0 118 }
samer@0 119
samer@0 120 public Component createLabel(String txt) {
samer@0 121 JLabel l=new JLabel(Node.lastPart(txt)) {
samer@0 122 public void setName(String txt) {
samer@0 123 super.setName(txt);
samer@0 124 setText(Node.lastPart(txt));
samer@0 125 setToolTipText(txt);
samer@0 126 }
samer@0 127 };
samer@0 128 l.setToolTipText(txt);
samer@0 129 MouseRetarget.mouseInvisibilityFor(l);
samer@0 130 return l;
samer@0 131 }
samer@0 132 public Viewer createViewerPanel(Viewer vwr) { return new VPanel(vwr); }
samer@0 133
samer@0 134 public Container createButtonsFor(Agent agent)
samer@0 135 {
samer@0 136 ButtonBar bbar=new ButtonBar();
samer@0 137 bbar.setTarget(agent);
samer@0 138 agent.getCommands( bbar);
samer@0 139 return bbar;
samer@0 140 }
samer@0 141
samer@0 142 public NumberViewer createNumberViewer(String label, int flags, NumberSink s) {
samer@0 143 return new TextualNumberViewer(label);
samer@0 144 }
samer@0 145
samer@0 146 public void exposeCommands( Agent agent) {
samer@0 147 buttonBar.setTarget(agent);
samer@0 148 agent.getCommands(buttonBar);
samer@0 149 }
samer@0 150
samer@0 151 // .......... Message printing ..............
samer@0 152 public void print(String msg) { System.out.println(msg); }
samer@0 153 public void status(String msg) { System.out.println(msg); }
samer@0 154
samer@0 155 private PrintWriter writer=null;
samer@0 156 public PrintWriter getPrintWriter()
samer@0 157 {
samer@0 158 if (writer==null)
samer@0 159 writer = new PrintWriter( new OutputStreamWriter(System.out));
samer@0 160 return writer;
samer@0 161 };
samer@0 162
samer@0 163 // public PrintWriter getPrintWriter() {
samer@0 164 // return new PrintWriter(con.getWriter(),true);
samer@0 165 // }
samer@0 166
samer@0 167 // public void print(String msg) { con.write(msg); con.write("\n"); }
samer@0 168 // public void status(String msg) { con.write(msg); con.write("\n"); }
samer@0 169
samer@0 170
samer@0 171 // ............... Private bits .............
samer@0 172
samer@0 173 // private JFrame currentFrame=null;
samer@0 174 private JFrame confr=null;
samer@0 175 private Console con=null;
samer@0 176 private ButtonBar buttonBar=null;
samer@0 177 private Component glue;
samer@0 178
samer@0 179 public static void main( String args[]) { new SwingShell(); }
samer@0 180 }
samer@0 181