📄 appwidget.java
字号:
/**
* The contents of this file are subject to the OAA Community Research
* License Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License
* at http://www.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999-2003. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.agt.startit;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class AppWidget extends JPanel implements StatusChangeListener {
private StatusLight light;
protected AppName nameBtn;
protected OptionBox options;
private HostWidget host;
private JButton helpBtn;
protected AppBox appBox;
protected AppInfo info;
protected JCheckBoxMenuItem hideMenuItem;
public AppWidget(AppBox box, AppInfo inf) {
appBox = box;
info = inf;
//setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
info.getProcess().addStatusChangeListener(this);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
int numCols = 0;
// CREATE: Status light on the extreme left
light = new StatusLight();
c.insets = new Insets(9, 4, 0, 0);
c.gridx = numCols++;
c.gridy = 0;
gridbag.setConstraints(light, c);
add(light);
/* This doesn't work - the menu btn doesn't grow inside its panel,
so if you click on the edges nothing happens....
// CREATE: Space under the App Name pulldown (nameBtn)
// (Only way I can be sure nameBtn is a minimum width: put a blank
// label under it (at the same grid point) with a set width)
// (gridbag seems to heed the setPreferredSize hint, but not
// setMinimumSize)
JLabel minSpace = new JLabel();
minSpace.setPreferredSize(new Dimension(200,1));
c.anchor = c.NORTHWEST;
c.gridx = 1;
c.fill = c.HORIZONTAL;
gridbag.setConstraints(minSpace, c);
add(minSpace);
*/
// CREATE: App Name pulldown (nameBtn)
options = new OptionBox(this);
nameBtn = new AppName(this);
c.anchor = c.NORTHWEST;
c.gridx = numCols++;
c.fill = c.HORIZONTAL;
gridbag.setConstraints(nameBtn, c);
add(nameBtn);
// if there's helptext for this app
if (info.helptext != null) {
// CREATE: helpBtn
helpBtn = new JButton("?");
helpBtn.setForeground(Color.yellow);
helpBtn.setBackground(Color.blue.darker());
helpBtn.setFont(new Font("SansSerif", Font.PLAIN, 18));
helpBtn.setMargin(new Insets(0,1,0,1));
helpBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// enclose helptext in html tags, so users can use things like <i>, <br>, etc.
JOptionPane.showMessageDialog(AppWidget.this, "<html>"+info.helptext+"</html>",
info.appname,
JOptionPane.INFORMATION_MESSAGE);
}
});
c.gridx = numCols++;
c.fill = c.NONE;
c.anchor = c.CENTER;
gridbag.setConstraints(helpBtn, c);
add(helpBtn);
}
// CREATE: Hostname selector
host = new HostWidget();
host.setHostname(info.host);
host.setUsername(info.user);
host.setShell(info.shell);
c.insets = new Insets(9, 30, 0, 4);
c.anchor = c.SOUTHEAST;
c.weightx = 1.0;
c.gridx = numCols++;
c.fill = c.NONE;
gridbag.setConstraints(host, c);
add(host);
// CREATE: Big options box containing all the selector widgets
// (was created above, since nameBtn needed to point to it)
c.weighty = 1.0;
c.insets = new Insets(4, 4, 9, 4);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = numCols-1;
c.fill = c.HORIZONTAL;
gridbag.setConstraints(options, c);
add(options);
// add the options to the OptionBox
for (Iterator i = info.options.iterator(); i.hasNext(); ) {
OptionInfo oinfo = (OptionInfo) i.next();
options.addWidget(oinfo);
}
}
protected void setHideMenuItem(JCheckBoxMenuItem i) {
hideMenuItem = i;
}
public void hide(boolean yn) {
hideMenuItem.setSelected(!yn);
info.hidden = yn;
appBox.showApps();
}
public void setBackground(Color c) {
super.setBackground(c);
if (nameBtn != null)
nameBtn.setBackground(c);
if (host != null)
host.setBackground(c);
}
public void updateInfo() { // update AppInfo from HostWidget
// 1: update host stuff
info.host = host.getHostname();
info.user = host.getUsername();
info.shell = host.getShell();
// 2: update options
options.updateInfo();
}
public void expandOptions(boolean grow) {
nameBtn.setExpanded(grow);
options.setExpanded(grow);
}
public void printStartCommands() {
appBox.updateStartitInfo();
info.printStartCommands();
}
public void start() {
appBox.updateStartitInfo();
info.start();
}
public void kill() {
info.getProcess().kill();
}
public void statusChanged(AppInfo info, int status) {
light.setStatus(status);
disableOptions(StartitProcess.isBusy(status));
}
protected void disableOptions(boolean yn) {
enableComponentTree(host, yn);
enableComponentTree(options, yn);
}
public static void enableComponentTree(Component p, boolean yn) {
p.setEnabled(!yn);
if (!(p instanceof Container)) return;
Container c = (Container) p;
int n = c.getComponentCount();
for(int i = 0; i < c.getComponentCount(); i++) {
Component kid = c.getComponent(i);
enableComponentTree(kid, yn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -