package realcix20.guis.components;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterPipeline;
import org.jdesktop.swingx.decorator.RolloverHighlighter;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Column;
import realcix20.guis.utils.DialogManager;
import realcix20.guis.utils.ImageManager;
import realcix20.guis.utils.Item;
import realcix20.guis.utils.MnemonicGenerator;
import realcix20.guis.utils.TxtManager;
import realcix20.guis.views.LayoutEditView;
import realcix20.guis.views.LayoutRenameView;
import realcix20.guis.views.MainView;
import realcix20.utils.DAO;
import realcix20.utils.ObjectUtil;
import realcix20.utils.Resources;
public class XTable extends JPanel implements ActionListener, MouseListener {
private JXTable table;
private JTable headerColumn;
private JComboBox searchField;
private JTextField searchText;
private JComboBox reportSelector;
private JButton searchButton;
private String current_sql;
private String report_basic_sql;
private BaseClass object;
private Map sortStates = new HashMap();
private final static ImageIcon REFRESH_IMAGE_ICON = new ImageIcon("./resources/images/refresh.png");
private final static ImageIcon SEARCH_IMAGE_ICON = new ImageIcon("./resources/images/search.png");
private MainView container;
private String stat;//report or object
private JPanel reportControlPanel;
private ArrayList<Action> reportOpts=new ArrayList<Action>();
private ListButton reportManageButton;
private int layoutId;
public XTable(BaseClass object, String report_basic_sql,int layoutId) {
this.object = object;
this.report_basic_sql = report_basic_sql;
this.current_sql = report_basic_sql;
this.stat = "report";
this.layoutId=layoutId;
initComponents();
sortStates.clear();
}
public XTable(BaseClass object) {
this.object = object;
this.current_sql = object.getBasicSQL();
this.stat = "object";
this.layoutId=0;
initComponents();
sortStates.clear();
}
//fix
public void setObject(BaseClass object) {
this.object = object;
this.current_sql = object.getBasicSQL();
sortStates.clear();
this.layoutId=0;
initTableProperties(current_sql);
initSearchField();
}
//fix
public void setReport_basic_sql(int layoutId,String report_basic_sql) {
this.report_basic_sql = report_basic_sql;
this.current_sql = report_basic_sql;
sortStates.clear();
this.layoutId=layoutId;
//fix
stat="report";
initTableProperties(current_sql);
initSearchField();
}
private void initComponents() {
setLayout(new GridBagLayout());
((GridBagLayout)getLayout()).columnWidths = new int[] {0, 0, 0};
((GridBagLayout)getLayout()).rowHeights = new int[] {25, 0, 0};
((GridBagLayout)getLayout()).columnWeights = new double[] {1.0, 1.0, 1.0E-4};
((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};
JPanel controllerPanel = new JPanel();
controllerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
searchField = new JComboBox();
searchField.setPreferredSize(new Dimension(150,20));
controllerPanel.add(searchField);
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
searchText = new JTextField();
searchText.setPreferredSize(new Dimension(100, 20));
searchPanel.add(searchText);
//fix
searchText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "search");
searchText.getActionMap().put("search", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
searchButton.doClick();
}});
searchButton = new JButton();
searchButton.setIcon(SEARCH_IMAGE_ICON);
searchButton.addActionListener(this);
searchButton.setActionCommand("SEARCH");
searchButton.setPreferredSize(new Dimension(20, 20));
searchPanel.add(searchButton);
controllerPanel.add(searchPanel);
reportControlPanel=new JPanel();
reportSelector = new JComboBox();
reportSelector.setPreferredSize(new Dimension(150, 20));
reportSelector.setActionCommand("REPORT SELECTORS");
updateReportSelector();
reportControlPanel.add(reportSelector);
//�������
reportManageButton = new ListButton();
reportManageButton.setPreferredSize(new Dimension(150,23));
Action defAction=new AbstractAction(){
public void actionPerformed(ActionEvent e) {
setDefault(getCurrentLayout());
}
};
defAction.putValue(Action.NAME, MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.SETDEFAULT"),KeyEvent.VK_S));
// defAction.putValue(Action.SMALL_ICON, ImageManager.getImage(ImageManager.NEW_REPORT_IMAGE));
Action addAction=new AbstractAction(){
public void actionPerformed(ActionEvent e) {
new LayoutEditView(getContainer(), -1, "ADD");
}
};
addAction.putValue(Action.NAME, MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.NEWREPORT"),KeyEvent.VK_N));
addAction.putValue(Action.SMALL_ICON, ImageManager.getImage(ImageManager.NEW_REPORT_IMAGE));
Action editAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
new LayoutEditView(getContainer(),getCurrentLayout(), "EDIT");
}
};
editAction.putValue(Action.NAME,MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.EDITREPORT"),KeyEvent.VK_E));
editAction.putValue(Action.SMALL_ICON, ImageManager.getImage(ImageManager.EDIT_REPORT_IMAGE));
reportOpts.add(editAction);
Action rnAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
new LayoutRenameView(XTable.this,getCurrentLayout());
}
};
rnAction.putValue(Action.NAME,MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.RENAMEREPORT"),KeyEvent.VK_R));
rnAction.putValue(Action.SMALL_ICON,ImageManager.getImage(ImageManager.RENAME_REPORT_IMAGE));
reportOpts.add(rnAction);
Action copyAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
new LayoutEditView(getContainer(),getCurrentLayout(), "COPY");
}
};
copyAction.putValue(Action.NAME,MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.COPYREPORT"),KeyEvent.VK_C));
copyAction.putValue(Action.SMALL_ICON, ImageManager.getImage(ImageManager.COPY_REPORT_IMAGE));
reportOpts.add(copyAction);
Action delAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if ( (DialogManager.showYesNoDialog(container, TxtManager.getTxt("MAINFRAME.DELETE.CONFIRM"))) == 0) {
realcix20.guis.layouts.LayoutManager.deleteLayout(getContainer().getCurrentObject().getClsId(),getCurrentLayout());
if(getCurrentLayout()==getContainer().getCurrentObject().getDefCL()){
setDefault(0);
layoutId=0;
getContainer().updateObjectView();
}else{
layoutId=getContainer().getCurrentObject().getDefCL();
getContainer().updateReportViewTable(layoutId);
}
}
}
};
delAction.putValue(Action.NAME,MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.DELETEREPORT"),KeyEvent.VK_D));
delAction.putValue(Action.SMALL_ICON,ImageManager.getImage(ImageManager.DELETE_REPORT_IMAGE));
reportOpts.add(delAction);
Action favAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
realcix20.guis.layouts.LayoutManager.updateReportIsFavorite(
getContainer().getCurrentObject().getClsId(),getCurrentLayout(), true);
getContainer().updateTree();
}
};
favAction.putValue(Action.NAME,MnemonicGenerator.generateMnemonicString(TxtManager.getTxt("VIEW.MAINFRAME.TREE.POPUPMENU.DAILYREPORTADD"),KeyEvent.VK_A));
favAction.putValue(Action.SMALL_ICON,ImageManager.getImage(ImageManager.DAILY_REPORT_ADD_IMAGE));
reportOpts.add(favAction);
//rmFavAction
// realcix20.guis.layouts.LayoutManager.updateReportIsFavorite(
// container.getContainer().getCurrentObject().getClsId(), layoutId, false);
// container.getContainer().updateTree();
reportManageButton.addAction(defAction);
reportManageButton.addAction(addAction);
reportManageButton.addAction(editAction);
reportManageButton.addAction(copyAction);
reportManageButton.addAction(rnAction);
reportManageButton.addAction(delAction);
reportManageButton.addAction(favAction);
reportControlPanel.add(reportManageButton);
controllerPanel.add(reportControlPanel);
add(controllerPanel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
table = new JXTable();
table.setSortable(false);
table.setEditable(false);
table.setHighlighters(new HighlighterPipeline(
new Highlighter[]{ AlternateRowHighlighter.genericGrey,
new RolloverHighlighter(Color.BLACK, Color.WHITE)}));
table.addMouseListener(this);
headerColumn = new JTable();
JTableHeader header = table.getTableHeader();
header.addMouseListener(this);
initTableProperties(current_sql);
JViewport jv = new JViewport();
jv.setView(headerColumn);
jv.setPreferredSize(new Dimension(50,20));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setRowHeader(jv);
scrollPane.setPreferredSize(new Dimension(500, 200));
scrollPane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER,headerColumn.getTableHeader());
scrollPane.setViewportView(table);
scrollPane.getViewport().setBackground(table.getBackground());
add(scrollPane, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 5, 5, 5), 0, 0));
initSearchField();
}
protected void setDefault(int layout) {
DAO dao = DAO.getInstance();
dao.update(Resources.UPDATE_CLS_DEFCL_SQL);
dao.setInt(1,layout);
dao.setInt(2,getContainer().getCurrentObject().getClsId());
dao.executeUpdate();
layoutId=layout;
}
public void updateReportSelector() {
reportSelector.removeActionListener(this);
reportSelector.removeAllItems();
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_CLS_ALL_LAYOUTS_SQL);
dao.setObject(1, object.getClsId());
ResultSet rs = dao.executeQuery();
try {
Item item = new Item(object.getClsId() + "." + "0", TxtManager.getTxt("VIEW.XTABLE.REPORTSELECTORALL"));
reportSelector.addItem(item);
if(layoutId==0) reportSelector.setSelectedItem(item);
while (rs.next()) {
String str = new String("CL." + object.getClsId() + "." + rs.getInt("LAYOUT"));
item = new Item(object.getClsId() + "." + rs.getInt("LAYOUT"), TxtManager.getTxt(str));
reportSelector.addItem(item);
if(layoutId!=0&&item.getFactValue().toString().endsWith(""+layoutId)){
reportSelector.setSelectedItem(item);
}
}
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
reportSelector.addActionListener(this);
reportSelector.repaint();
}
private void initTableProperties(String sql) {
CacheTableModel tm = new CacheTableModel(object, sql, stat);
TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
boolean first = true;
public void addColumn(TableColumn tc) {
if (first) {
super.addColumn(tc);
first = false;
}
}
};
headerColumn.setModel(tm);
headerColumn.setColumnModel(rowHeaderModel);
headerColumn.createDefaultColumnsFromModel();
headerColumn.setEnabled(false);
TableColumn indexColumn = headerColumn.getColumnModel().getColumn(0);
indexColumn.setCellRenderer(new IndexRenderer());
indexColumn.setMinWidth(50);
indexColumn.setResizable(false);
headerColumn.setGridColor(Color.LIGHT_GRAY);
headerColumn.setRowHeight(18);
table.setRowHeight(18);
table.setGridColor(Color.LIGHT_GRAY);
TableColumnModel cm = new DefaultTableColumnModel() {
boolean first = true;
public void addColumn(TableColumn tc) {
if (first) {
first = false;
return;
}
tc.setResizable(true);
super.addColumn(tc);
}
};
table.setModel(tm);
table.setColumnModel(cm);
table.createDefaultColumnsFromModel();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
TableColumn column = table.getColumnModel().getColumn(i);
String columnName = (String)column.getHeaderValue();
if (sortStates.get(columnName) == null)
column.setHeaderRenderer(new SortRenderer(null, -1));
}
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
TableColumn tcolumn = table.getColumnModel().getColumn(i);
//String columnName = (String)tcolumn.getHeaderValue();
//fort report table
if (((String)tm.getTables().get(i)).trim().length() <= 0) {
String name = (String)tm.getColumns().get(i);
String value = null;
StringTokenizer st = new StringTokenizer(name, "_");
st.nextElement();
String tableName = (String)st.nextElement();
String columnName = (String)st.nextElement();
Column column = ObjectUtil.findColumn(object, tableName, columnName);
if (column != null)
value = column.toString();
else
value = tableName + "_" + columnName;
if (name.indexOf("SUM_") > -1)
tcolumn.setHeaderRenderer(new ImageRenderer("SUM(" + value + ")", ImageManager.getImage(ImageManager.SUM_IMAGE)));
else if (name.indexOf("COUNT_") > -1)
tcolumn.setHeaderRenderer(new ImageRenderer("COUNT(" + value + ")", ImageManager.getImage(ImageManager.COUNT_IMAGE)));
}
}
try {
for (int i = 1; i < tm.getColumnCount(); i++) {
if (tm.getRs().getMetaData().getColumnDisplaySize(i) == 6)
cm.getColumn(i - 1).setPreferredWidth(150);
else if (tm.getRs().getMetaData().getColumnDisplaySize(i) >= 256)
cm.getColumn(i -1).setPreferredWidth(100);
else
cm.getColumn(i - 1).setMinWidth(tm.getRs().getMetaData().getColumnDisplaySize(i) * 5);
cm.getColumn(i - 1).setMaxWidth(200);
}
} catch (Exception e) {
e.printStackTrace();
}
int n = 0;
Iterator keyIter = sortStates.keySet().iterator();
CacheTableModel model = (CacheTableModel)table.getModel();
if (stat.equals("object")) {
while (keyIter.hasNext()) {
Column column = (Column)keyIter.next();
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
String columnName = (String)table.getColumnModel().getColumn(i).getHeaderValue();
if (column.toString().equals(columnName)) {
table.getColumnModel().getColumn(i).setHeaderRenderer(new SortRenderer((String)sortStates.get(column), n + 1));
break;
}
}
n++;
}
} else if (stat.equals("report")) {
while (keyIter.hasNext()) {
String sortColumn = (String)keyIter.next();
if ( (sortColumn.indexOf("SUM_") == -1) && (sortColumn.indexOf("COUNT_") == -1) ) {
StringTokenizer st = new StringTokenizer(sortColumn, ".");
String tableName = (String)st.nextElement();
String columnName = (String)st.nextElement();
for (int i = 0; i < model.getTables().size(); i++) {
if ( (tableName.equals(model.getTables().get(i))) &&
(columnName.equals(model.getColumns().get(i))) ) {
table.getColumnModel().getColumn(i).setHeaderRenderer(new SortRenderer(
(String)sortStates.get(sortColumn), n));
}
}
}
}
}
table.setSelectionModel(headerColumn.getSelectionModel());
updateReportSelector();
}
private void initSearchField() {
searchField.removeAllItems();
CacheTableModel model = (CacheTableModel)table.getModel();
if (stat.equals("object")) {
for (int i = 0; i < model.getColumnCount(); i++) {
if (i > 0) {
searchField.addItem(model.getColumn(i));
sortStates.put(model.getColumn(i), null);
}
}
} else if (stat.equals("report")) {
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
String name = (String)model.getColumns().get(i);
String id = null;
String value = null;
String tableName = null;
String columnName = null;
if (((String)model.getTables().get(i)).trim().length() <= 0) {
StringTokenizer st = new StringTokenizer(name, "_");
st.nextElement();
tableName = (String)st.nextElement();
columnName = (String)st.nextElement();
} else {
tableName = (String)model.getTables().get(i);
columnName = (String)model.getColumns().get(i);
}
Column column = ObjectUtil.findColumn(object, tableName, columnName);
if (column != null)
value = column.toString();
else
value = tableName + "_" + columnName;
id = tableName + "." + columnName;
if (name.indexOf("SUM_") > -1) {
id = "SUM_" + id;
value = "SUM(" + value + ")";
}
else if (name.indexOf("COUNT_") > -1) {
id = "COUNT_" + id;
value = "COUNT(" + value + ")";
}
Item item = new Item(id, value);
searchField.addItem(item);
sortStates.put(item, null);
}
}
}
private void search() {
sortStates.clear();
if (stat.equals("object")) {
if (searchText.getText().trim().length() > 0) {
Column selectColumn = (Column)searchField.getSelectedItem();
//fix
current_sql = object.getBasicSQL().concat(" WHERE UPPER(" + selectColumn.getTableName() + "." +
selectColumn.getColumnName() + ") LIKE " + "'%" + searchText.getText().trim().toUpperCase() + "%'");
initTableProperties(current_sql);
} else {
initTableProperties(object.getBasicSQL());
}
} else if (stat.equals("report")) {
if (searchText.getText().trim().length() > 0) {
Item selectItem = (Item)searchField.getSelectedItem();
current_sql = generateReportSql(selectItem.getFactValue().toString(), searchText.getText().trim());
initTableProperties(current_sql);
} else {
initTableProperties(report_basic_sql);
}
}
}
private String generateReportSql(String filterItem, String filterString) {
String sql = report_basic_sql;
if ( (filterItem.startsWith("SUM_")) || (filterItem.startsWith("COUNT_")) ) {
if (filterItem.startsWith("SUM_")) {
filterItem = "SUM" + "(" + filterItem.substring(4) + ")";
} else if (filterItem.startsWith("COUNT_")) {
filterItem = "COUNT" + "(" + filterItem.substring(6) + ")";
}
if (report_basic_sql.indexOf("HAVING") > -1) {
String insert_sql = " UPPER(" + filterItem + ") LIKE " + "'%" + filterString.toUpperCase() + "%'" + " AND ";
StringBuffer sb = new StringBuffer(report_basic_sql.substring(0, report_basic_sql.indexOf("HAVING") + 6));
sb.append(insert_sql);
sb.append(report_basic_sql.substring(report_basic_sql.indexOf("HAVING") + 6));
sql = sb.toString();
} else {
String insert_sql = " HAVING UPPER(" + filterItem + ") LIKE " + "'%" + filterString.toUpperCase() + "%'" + " ";
if (report_basic_sql.indexOf("ORDER BY") > -1) {
StringBuffer sb = new StringBuffer(report_basic_sql.substring(
0, report_basic_sql.indexOf("ORDER BY")));
sb.append(insert_sql);
sb.append(report_basic_sql.substring(report_basic_sql.indexOf("ORDER BY")));
sql = sb.toString();
} else {
StringBuffer sb = new StringBuffer(report_basic_sql);
sb.append(insert_sql);
sql = sb.toString();
}
}
} else {
// if (report_basic_sql.indexOf("WHERE")!=-1) {
// String insert_sql = " UPPER(" + filterItem + ") LIKE " + "'%" + filterString.toUpperCase() + "%'" + " AND ";
// StringBuffer sb = new StringBuffer(report_basic_sql.substring(0, report_basic_sql.indexOf("WHERE") + 5));
// sb.append(insert_sql);
// sb.append(report_basic_sql.substring(report_basic_sql.indexOf("WHERE") + 5));
// sql = sb.toString();
// } else {
String insert_sql = " WHERE UPPER(" + filterItem + ") LIKE " + "'%" + filterString.toUpperCase() + "%'" + " ";
if (report_basic_sql.indexOf("GROUP BY") > -1) {
StringBuffer sb = new StringBuffer(report_basic_sql.substring(
0, report_basic_sql.indexOf("GROUP BY")));
sb.append(insert_sql);
sb.append(report_basic_sql.substring(report_basic_sql.indexOf("GROUP BY")));
sql = sb.toString();
} else {
if (report_basic_sql.indexOf("ORDER BY") > -1) {
StringBuffer sb = new StringBuffer(report_basic_sql.substring(
0, report_basic_sql.indexOf("ORDER BY")));
sb.append(insert_sql);
sb.append(report_basic_sql.substring(report_basic_sql.indexOf("ORDER BY")));
sql = sb.toString();
} else {
StringBuffer sb = new StringBuffer(report_basic_sql);
sb.append(insert_sql);
sql = sb.toString();
}
// }
}
}
// System.err.println("sql = " + sql);
return sql;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("SEARCH")) {
search();
}else if(e.getActionCommand().equals("REPORT SELECTORS")){
layoutId=getCurrentLayout();
if(getCurrentLayout()==0){
//���DZ���
getContainer().updateObjectView();
}else{
getContainer().updateLayoutView(getCurrentLayout());
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
private void sortObjectTable(MouseEvent e) {
TableColumnModel colModel = table.getColumnModel();
int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
int modelIndex = colModel.getColumn(columnModelIndex)
.getModelIndex();
CacheTableModel model = (CacheTableModel)table.getModel();
Column column = model.getColumn(modelIndex);
String sortOrder = "ASC";
if (sortStates.get(column) != null) {
if (sortStates.get(column).equals("DESC"))
sortStates.remove(column);
else if (sortStates.get(column).equals("ASC")) {
sortOrder = "DESC";
sortStates.put(column, sortOrder);
}
} else {
sortStates.clear();
sortStates.put(column, sortOrder);
}
Iterator keyIter = sortStates.keySet().iterator();
int n = 0;
String sortSql = current_sql;
while (keyIter.hasNext()) {
Column tempColumn = (Column)keyIter.next();
if (n == 0)
sortSql = sortSql.concat(" ORDER BY " + tempColumn.getTableName() + "." +
tempColumn.getColumnName() + " " + sortStates.get(tempColumn));
else
sortSql = sortSql.concat(", " + tempColumn.getTableName() + "." +
tempColumn.getColumnName() + " " + sortStates.get(tempColumn));
n++;
}
initTableProperties(sortSql);
}
private void sortReportTable(MouseEvent e) {
TableColumnModel colModel = table.getColumnModel();
int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
int modelIndex = colModel.getColumn(columnModelIndex)
.getModelIndex();
CacheTableModel model = (CacheTableModel)table.getModel();
String tableName = (String)model.getTables().get(modelIndex - 1);
String columnName = (String)model.getColumns().get(modelIndex - 1);
// System.out.println(tableName + "/" + columnName);
String sortColumn = null;
if (tableName.trim().length() > 0)
sortColumn = tableName + "." + columnName;
else
sortColumn = columnName;
String sortOrder = "ASC";
if (sortStates.get(sortColumn) != null) {
if (sortStates.get(sortColumn).equals("DESC"))
sortStates.remove(sortColumn);
else if (sortStates.get(sortColumn).equals("ASC")) {
sortOrder = "DESC";
sortStates.put(sortColumn, sortOrder);
}
} else {
sortStates.clear();
sortStates.put(sortColumn, sortOrder);
}
Iterator keyIter = sortStates.keySet().iterator();
String sort_sql = null;
StringBuffer sb = null;
if (current_sql.indexOf("ORDER BY") > -1)
sb = new StringBuffer(current_sql.substring(
0, current_sql.indexOf("ORDER BY")));
else
sb = new StringBuffer(current_sql);
sort_sql = sb.toString();
while (keyIter.hasNext()) {
sortColumn = (String)keyIter.next();
String insert_sql = " ORDER BY " + sortColumn + " " + sortStates.get(sortColumn);
sb.append(insert_sql);
sort_sql = sb.toString();
}
initTableProperties(sort_sql);
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() instanceof JTableHeader) {
if (stat.equals("object"))
sortObjectTable(e);
else if (stat.equals("report"))
sortReportTable(e);
}
}
public JTable getTable() {
return table;
}
public void setContainer(MainView container) {
this.container = container;
}
class ImageRenderer extends JLabel implements TableCellRenderer {
public ImageRenderer(String text, ImageIcon icon) {
super(text, icon, JLabel.CENTER);
setOpaque(true);
}
public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
setHorizontalAlignment(SwingConstants.CENTER);
setBorder(new LineBorder(Color.lightGray));
return this;
}
}
public void enableReportControl(boolean enable){
reportControlPanel.setVisible(enable);
}
public void enableReportOpt(boolean enable){
reportManageButton.setAction(reportManageButton.getActions().get(0));
for (Action act : reportOpts) {
act.setEnabled(enable);
}
}
public MainView getContainer() {
return container;
}
public int getCurrentLayout() {
String s = ((Item)reportSelector.getSelectedItem()).getFactValue().toString();
return Integer.parseInt(s.substring(s.indexOf('.')+1));
}
}