//This file is part of SECONDO.
//Copyright (C) 2004, University in Hagen, Department of Computer Science,
//Database Systems for New Applications.
//SECONDO is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2 of the License, or
//(at your option) any later version.
//SECONDO is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with SECONDO; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package viewer.hoese;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sj.lang.ListExpr;
import sj.lang.ServerErrorCodes;
import java.util.Properties;
import java.util.*;
import javax.swing.event.*;
import viewer.HoeseViewer;
import tools.Reporter;
import gui.SecondoObject;
/**
* This class displays the textual results of a query
*/
public class TextWindow extends JPanel {
/** Allows scrolling over the query result */
private JScrollPane QueryScrollPane;
/** A ComboBox of all query or import results */
private JComboBox QueryCombo;
/** The main app. */
private HoeseViewer parent;
/** The Code for no error */
private static final int NOT_ERROR_CODE = ServerErrorCodes.NOT_ERROR_CODE;
/** a dummy for empty display */
private JPanel dummy = new JPanel();
/** Components for a search Panel */
private JTextField SearchText;
private JButton SearchBtn;
/**
* Construktor
* @param MainWindow aparent
* @see Source
*/
public TextWindow (HoeseViewer aparent) {
super();
setLayout(new BorderLayout());
QueryCombo = new JComboBox(new DefaultComboBoxModel());
QueryCombo.setMaximumSize(new Dimension(200, 300));
setMinimumSize(new Dimension(100, 100));
QueryScrollPane = new JScrollPane();
add(QueryCombo, BorderLayout.NORTH);
add(QueryScrollPane, BorderLayout.CENTER);
QueryCombo.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt) {
QueryResult qr = null;
qr = (QueryResult)QueryCombo.getSelectedItem();
if (qr != null){
qr.clearSelection();
QueryScrollPane.setViewportView(qr);
}
else
QueryScrollPane.setViewportView(dummy);
}
});
parent = aparent;
//construct a search panel
JPanel SearchPanel = new JPanel();
SearchBtn = new JButton("go");
JLabel SearchLabel = new JLabel("search");
SearchText= new JTextField(6);
SearchPanel.add(SearchLabel);
SearchPanel.add(SearchText);
SearchPanel.add(SearchBtn);
add(SearchPanel,BorderLayout.SOUTH);
SearchBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
search();
}
});
SearchText.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_ENTER)
search();
}
});
}
public void ensureSelectedIndexIsVisible(){
QueryResult qr = (QueryResult) QueryCombo.getSelectedItem();
if(qr==null)
return;
int Pos = qr.getSelectedIndex();
if(Pos>=0){
int h = QueryScrollPane.getSize().height;
int fh = qr.getFont().getSize();
int rows = h/(fh+4); // ca. number of visible rows (+4 = gap between rows)
rows = rows/2 -1; // the rows under and above from Pos;
int Count = qr.getModel().getSize();
if(PosCount)
qr.ensureIndexIsVisible(Count);
else{
qr.ensureIndexIsVisible(Pos-rows);
qr.ensureIndexIsVisible(Pos+rows);
}
}
}
private void search(){
QueryResult QR = (QueryResult) QueryCombo.getSelectedItem();
if(QR==null){
Reporter.showError("no query result selected");
return;
}
String Text=SearchText.getText().trim();
if(Text.equals("")){
Reporter.showError("no text to search entered");
return;
}
int SelectedIndex = QR.getSelectedIndex();
int Offset= (SelectedIndex<0)?0:SelectedIndex+1;
int Pos = QR.find(Text,false,Offset);
if (Pos<0)
Pos=QR.find(Text,false,1);
if(Pos<0){
Reporter.showError("text not found");
return;
}
QR.setSelectedIndex(Pos);
int h = QueryScrollPane.getSize().height;
int fh = QR.getFont().getSize();
int rows = h/(fh+4); // ca. number of visible rows (+4 = gap between rows)
rows = rows/2 -1; // the rows under and above from Pos;
int Count = QR.getModel().getSize();
if(PosCount)
QR.ensureIndexIsVisible(Count);
else{
QR.ensureIndexIsVisible(Pos-rows);
QR.ensureIndexIsVisible(Pos+rows);
}
}
/* set a new ComboBox() */
public void clearComboBox(){
remove(QueryCombo);
QueryCombo = new JComboBox();
QueryCombo.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt) {
QueryResult qr = null;
qr = (QueryResult)QueryCombo.getSelectedItem();
if (qr != null){
qr.clearSelection();
QueryScrollPane.setViewportView(qr);
}
else
QueryScrollPane.setViewportView(dummy);
}
});
add(QueryCombo, BorderLayout.NORTH);
}
/**
* Converts a QueryResult to a listexpr. Used in session-saving
* @param qr The queryresult to convert
* @return The result as a ListExpr
* @see Source
*/
private ListExpr convertQueryResulttoLE (QueryResult qr) {
ListExpr catl = ListExpr.theEmptyList();
ListExpr left = catl;
int labno=0;
int rendno=0;
for (int i = 0; i < qr.getGraphObjects().size(); i++) {
DsplGraph dg = (DsplGraph)qr.getGraphObjects().elementAt(i);
int catnr = parent.Cats.indexOf(dg.getCategory());
LabelAttribute la = dg.getLabelAttribute();
RenderAttribute ra = dg.getRenderAttribute();
// find the objects within the query result
labno=-1;
rendno=-1;
boolean doneLab = (la==null) || (la instanceof DefaultLabelAttribute);
boolean doneRend = (ra==null) || (ra instanceof DefaultRenderAttribute);
if(ra instanceof DefaultRenderAttribute){
rendno = -2;
}
ListModel lm = qr.getModel();
int p = 0;
int size = lm.getSize();
while(pSource
*/
public ListExpr convertAllQueryResults () {
QueryResult qr = (QueryResult)QueryCombo.getSelectedItem();
if (qr != null)
qr.clearSelection();
ListExpr le = ListExpr.theEmptyList();
ListExpr left = le;
for (int i = 0; i < QueryCombo.getItemCount(); i++)
if (le.isEmpty()) {
left = ListExpr.cons(convertQueryResulttoLE((QueryResult)QueryCombo.getItemAt(i)),
le);
le = left;
}
else
left = ListExpr.append(left, convertQueryResulttoLE((QueryResult)QueryCombo.getItemAt(i)));
return ListExpr.twoElemList(ListExpr.symbolAtom("QueryResults"), le);
}
/** Method supporting the readAllQueryResults method **/
private void assignCatsAndLayersOldVersion(Vector Layers,
QueryResult qr,
ListExpr catList,
ListExpr layerList){
Iterator li = qr.getGraphObjects().iterator();
while (li.hasNext()) {
DsplGraph dg = (DsplGraph)li.next();
setGOtoLayerPos(Layers,
dg,
catList.first().intValue(),
layerList.first().intValue(),
layerList.second().intValue());
catList = catList.rest();
String label = catList.first().stringValue();
if(label.equals("")){
dg.setLabelAttribute(null);
}
dg.setLabelAttribute(new DefaultLabelAttribute(label));
catList = catList.rest();
dg.getLabPosOffset().setLocation(catList.first().realValue(), catList.second().realValue());
catList = catList.rest().rest();
dg.setVisible(catList.first().boolValue());
catList = catList.rest();
layerList = layerList.rest().rest();
}
}
/** Method supporting the readAllQueryResult method.
**/
private void assignCatsAndLayers(Vector Layers, QueryResult qr,
ListExpr catList, ListExpr layerList){
Vector GraphObjects = qr.getGraphObjects();
if(catList.atomType()!=ListExpr.NO_ATOM){ // wromg format
return;
}
if(layerList.atomType()!=ListExpr.NO_ATOM){ // wrong format
return;
}
int size = GraphObjects.size();
// method stops if an error occurs
ListModel lm = qr.getModel();
int catno;
String label;
double xOffset;
double yOffset;
boolean visible;
int la_no;
int ra_no;
int layerno;
int layerpos;
// scan all GraphObjects contained in qr
for(int i=0;i 0)
parent.addSwitch(parent.GraphDisplay.addLayer(lay, true), -1);
}
return true;
}
/**GOs will be placed on its original position before saving.Used in Session-saving
* @see Source
*/
private void setGOtoLayerPos (Vector layers,
DsplGraph dg,
int catnr,
int laynr,
int laypos) {
// extend Layers by enough elements
while (layers.size() <= laynr){
layers.add(new Layer());
}
// get the layer selected in argument
Layer lay = (Layer)layers.elementAt(laynr);
// add GeoObjects to fill the layer
while (lay.getGeoObjects().size() <= laypos){
lay.getGeoObjects().add(null);
}
// assign the category
int maxCats = parent.Cats.size();
if(catnr < maxCats){
dg.setCategory((Category)parent.Cats.elementAt(catnr));
} else{
Reporter.writeError("try to assign category number "+ catnr+
" but only " + maxCats+" categories exist");
if(maxCats>0){
dg.setCategory((Category)parent.Cats.get(0));
} else{
Reporter.writeError("panik, cannot find any categories !");
}
}
// insert into layer
lay.getGeoObjects().setElementAt(dg, laypos);
}
/**
* Gets the ComboBox where all query-results are listed
* @return QueryCombo
* @see Source
*/
public JComboBox getQueryCombo () {
return QueryCombo;
}
/**
* Process the query qr an adds it to the QueryComboBox
* @param qr
* @return A list with no error.
* @see Source
*/
public ListExpr newQueryResult (QueryResult qr) {
ListExpr numericType;
ListExpr answerList;
if (qr.LEResult.isEmpty()) {
answerList = ListExpr.twoElemList(ListExpr.intAtom(NOT_ERROR_CODE), ListExpr.theEmptyList());
return answerList;
}
if (qr.LEResult.listLength() != 2) {
// If the queryResult is not a two elements list.
Reporter.writeError("laenge nicht 2");
qr.addEntry(new String(qr.LEResult.writeListExprToString()));
answerList = ListExpr.twoElemList(ListExpr.intAtom(NOT_ERROR_CODE), ListExpr.theEmptyList());
return answerList;
}
processQuery(qr);
addQueryResult(qr);
QueryCombo.setSelectedItem(qr);
// QueryScrollPane.setViewportView(qr);
answerList = ListExpr.twoElemList(ListExpr.intAtom(NOT_ERROR_CODE), ListExpr.theEmptyList());
return answerList;
}
/**
* Adds qr to the Combo-Box of queries
* @param qr
*/
public void addQueryResult (QueryResult qr) {
QueryResult q = (QueryResult)QueryCombo.getSelectedItem();
if (q != null)
q.clearSelection();
qr.setToolTipText(qr.toString());
QueryCombo.addItem(qr);
// ensure that the querycombo has the same background like
// its environment
QueryCombo.setOpaque(this.isOpaque());
QueryCombo.setBackground(this.getBackground());
}
/** Starts scanning of the query result qr for datatypes
*/
private void processQuery (QueryResult qr) {
String name = qr.LEResult.first().toString();
LEUtils.analyse(name,0,0,qr.LEResult.first(), qr.LEResult.second(), qr);
qr.computeTimeBounds();
}
}