📄 stopmonitor.java
字号:
{
super(myMonitor.parent);
this.myMonitor = myMonitor;
model = new DefaultListModel();
list = new JList(model);
for (int i=0; i<queryBrokers.size(); i++)
model.addElement(queryBrokers.get(i));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectionModel(new CBSingleSelectionModel(list)); //TE: tries to ensure that the selected item is visible.
list.setCellRenderer(new StopMonitorListRenderer());
list.setVisibleRowCount(6);
list.setSelectedIndex(0);
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().setView(list);
getContentPane().add("Center", scrollPane);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
CBButton ok, cancel, help;
buttonPanel.add(ok = new CBButton(CBIntText.get("Delete Query"), CBIntText.get("Delete the current query.")));
buttonPanel.add(cancel = new CBButton(CBIntText.get("Exit"), CBIntText.get("Exit this window.")));
buttonPanel.add(help = new CBButton(CBIntText.get("Help"), CBIntText.get("Open the help.")));
CBHelpSystem.useDefaultHelp(help, HelpIDs.ABORT); //TE: direct link to the topic in the help.
getContentPane().add("South", buttonPanel);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cancelSelectedQuery(); // get the selected query and cancel it.
}
});
cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2) cancelSelectedQuery(); // get the selected query and cancel it.
}
});
}
/**
* Updates the list of queries/Broker pairs, maintaining the selection (if possible)
*/
protected void update()
{
int selectedID = -1;
Vector queryBrokers = myMonitor.getQueryBrokers();
QueryBroker selectedQuery = (QueryBroker)list.getSelectedValue(); // cache currently selected query
if (selectedQuery != null)
selectedID = selectedQuery.id;
model.removeAllElements();
for (int i=0; i<queryBrokers.size(); i++)
{
QueryBroker newQuery = (QueryBroker)queryBrokers.get(i);
model.addElement(newQuery);
if (selectedID == newQuery.id)
list.setSelectedValue(newQuery, true); // re-assert selection
}
}
/**
* Called when the user has selected an item, and either double clicked
* or hit 'o.k.'.
*/
protected void cancelSelectedQuery()
{
synchronized(this)
{
if (list.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(this.getContentPane(),
CBIntText.get("You must select a query to stop from the list") + "\n" +
CBIntText.get("otherwise you can just use the 'cancel' button."),
CBIntText.get("Stop Monitor Help"), JOptionPane.INFORMATION_MESSAGE);
}
else
{
selectedQuery = (QueryBroker)list.getSelectedValue();
selected = true;
model.removeElement(selectedQuery);
notify();
}
}
}
public void setVisible(boolean status)
{
super.setVisible(status);
synchronized(this)
{
if (status == true) notify(); // wake up status polling thread.
}
}
/**
* Checks if there is only a single query to delete. If there is, it
* deletes it and returns true. Otherwise it sets the selected index
* to zero, and returns false.
* @return whether there was only a single query, now deleted.
*/
public boolean singleDelete()
{
if (model.size() == 0) return false; // *huh*? shouldn't ever happen.
list.setSelectedIndex(0);
if (model.size() == 1)
{
cancelSelectedQuery();
return true;
}
return false;
}
/**
* Do thread magic. This loops, waiting until it is
* notified that selectedQuery has changed. It will
* then attempt to delete that query. While the monitor
* is visible it should also poll the queue status every
* 250 ms for changes.
*/
public void run()
{
while (!finished)
{
try
{
synchronized(this)
{
if (selectedQuery != null)
{
myMonitor.cancelQuery(selectedQuery); // cancel query
selectedQuery = null; // and reset 'query to cancel' variable.
}
if (isVisible()) // we want to keep polling the task list while active.
wait(250);
else
wait();
update();
}
}
catch (InterruptedException e)
{
CBUtility.error("Exception in stop monitor: ", e);
}
}
}
}
//
// END StopMonitorGUI class
//
//
// START StopMonitorListRenderer class
//
/**
* A quicky cell renderer to allow us to display active and pending
* queries in different colours, and to display the text of a
* QueryBroker object.
*/
class StopMonitorListRenderer extends JLabel implements ListCellRenderer
{
Color highlight = new Color(0,0,128);
Color active = new Color(128,0,0);
Color pending = new Color(0,128,0);
Color cancelled = new Color(64,64,64);
StopMonitorListRenderer()
{
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus)
{
if (value instanceof QueryBroker == false) // should never happen :-)
{
log.warning("Rendering error in StopMonitor"); setText("error"); return this;
}
if (index == -1)
{
int selected = list.getSelectedIndex();
if (selected == -1)
return this;
else
index = selected;
}
DataQuery item = ((QueryBroker)value).query;
if (item == null) // shouldn't happen
{
setBackground(Color.white);
setForeground(cancelled);
setText("<deleted>");
return this;
}
setText(item.toString());
Color back = Color.white;
Color fore = Color.black;
if (item.isCancelled())
fore = cancelled;
else if (item.isRunning())
fore = active;
else
fore = pending;
if (isSelected)
{
setBackground(highlight);
setForeground(Color.white);
}
else
{
setBackground(back);
setForeground(fore);
}
return this;
}
}
//
// STOP StopMonitorListRenderer class
//
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -