001: /*
002: * CompileDbObjectAction.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.actions;
013:
014: import java.awt.EventQueue;
015: import java.awt.event.ActionEvent;
016: import java.sql.SQLException;
017: import java.util.ArrayList;
018: import java.util.List;
019: import javax.swing.JMenuItem;
020: import javax.swing.ListSelectionModel;
021: import javax.swing.SwingUtilities;
022: import javax.swing.event.ListSelectionEvent;
023: import javax.swing.event.ListSelectionListener;
024: import workbench.db.DbObject;
025: import workbench.db.WbConnection;
026: import workbench.db.oracle.OracleObjectCompiler;
027: import workbench.gui.WbSwingUtilities;
028: import workbench.gui.dbobjects.DbObjectList;
029: import workbench.gui.dbobjects.ObjectCompilerUI;
030: import workbench.log.LogMgr;
031:
032: /**
033: * @author support@sql-workbench.net
034: */
035: public class CompileDbObjectAction extends WbAction implements
036: ListSelectionListener {
037: private JMenuItem menuItem;
038: private DbObjectList source;
039: private ListSelectionModel selection;
040:
041: public CompileDbObjectAction(DbObjectList client,
042: ListSelectionModel list) {
043: this .initMenuDefinition("MnuTxtRecompile");
044: this .source = client;
045: this .selection = list;
046: setVisible(false);
047: setEnabled(false);
048: }
049:
050: public void setVisible(boolean flag) {
051: if (this .menuItem == null) {
052: menuItem = getMenuItem();
053: }
054: menuItem.setVisible(flag);
055: }
056:
057: public void setConnection(WbConnection conn) {
058: if (conn != null && conn.getMetadata().isOracle()) {
059: this .setVisible(true);
060: selection.addListSelectionListener(this );
061: checkState();
062: } else {
063: selection.removeListSelectionListener(this );
064: this .setVisible(false);
065: this .setEnabled(false);
066: }
067: }
068:
069: @Override
070: public void executeAction(ActionEvent e) {
071: compileObjects();
072: }
073:
074: private void compileObjects() {
075: if (!WbSwingUtilities.checkConnection(source.getComponent(),
076: source.getConnection()))
077: return;
078:
079: List<DbObject> objects = getSelectedObjects();
080: if (objects == null || objects.size() == 0)
081: return;
082:
083: try {
084: ObjectCompilerUI compilerUI = new ObjectCompilerUI(objects,
085: this .source.getConnection());
086: compilerUI.show(SwingUtilities.getWindowAncestor(source
087: .getComponent()));
088: } catch (SQLException e) {
089: LogMgr.logError("ProcedureListPanel.compileObjects()",
090: "Error initializing ObjectCompilerUI", e);
091: }
092: }
093:
094: private List<DbObject> getSelectedObjects() {
095: List<? extends DbObject> selected = this .source
096: .getSelectedObjects();
097: if (selected == null || selected.size() == 0)
098: return null;
099:
100: List<DbObject> objects = new ArrayList<DbObject>(selected
101: .size());
102: for (DbObject dbo : selected) {
103: if (OracleObjectCompiler.canCompile(dbo)) {
104: objects.add(dbo);
105: }
106: }
107: return objects;
108: }
109:
110: private void checkState() {
111: List<DbObject> selected = getSelectedObjects();
112: this .setEnabled(selected != null && selected.size() > 0);
113: }
114:
115: public void valueChanged(ListSelectionEvent e) {
116: EventQueue.invokeLater(new Runnable() {
117: public void run() {
118: checkState();
119: }
120: });
121: }
122:
123: }
|