You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1139 lines
43 KiB
1139 lines
43 KiB
package de.superx.dbadmin; |
|
|
|
import java.awt.BorderLayout; |
|
import java.awt.Color; |
|
import java.awt.Container; |
|
import java.awt.Font; |
|
import java.awt.Frame; |
|
import java.awt.Graphics; |
|
import java.awt.Graphics2D; |
|
import java.awt.GridLayout; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.awt.event.KeyEvent; |
|
import java.awt.event.KeyListener; |
|
import java.awt.print.PageFormat; |
|
import java.awt.print.Paper; |
|
import java.awt.print.Printable; |
|
import java.awt.print.PrinterException; |
|
import java.awt.print.PrinterJob; |
|
import java.sql.SQLException; |
|
import java.text.DateFormat; |
|
import java.text.ParseException; |
|
import java.util.Calendar; |
|
import java.util.Enumeration; |
|
import java.util.GregorianCalendar; |
|
import java.util.TimeZone; |
|
import java.util.Vector; |
|
|
|
import javax.swing.JButton; |
|
import javax.swing.JLabel; |
|
import javax.swing.JOptionPane; |
|
import javax.swing.JPanel; |
|
import javax.swing.JScrollPane; |
|
import javax.swing.JTextField; |
|
import javax.swing.tree.DefaultMutableTreeNode; |
|
import javax.swing.tree.DefaultTreeCellRenderer; |
|
import javax.swing.tree.DefaultTreeModel; |
|
import javax.swing.tree.MutableTreeNode; |
|
import javax.swing.tree.TreePath; |
|
import javax.swing.tree.TreeSelectionModel; |
|
|
|
import de.memtext.util.DateUtils; |
|
import de.memtext.util.WindowUtils; |
|
import de.memtext.widgets.InfoMessage; |
|
|
|
class AdminOrga extends JPanel implements Printable, ActionListener, |
|
KeyListener { |
|
private boolean result; |
|
|
|
private boolean isSaveNecessary = false; |
|
|
|
private OrgaEintrag2 gewaehlt, einEintrag; |
|
private Vector orga, merkungültig; |
|
private int entryCount; |
|
private JLabel standLbl; |
|
private GregorianCalendar cal = new GregorianCalendar(); |
|
private String standDatum; |
|
private JTextField suchtext; |
|
private Container cp; |
|
private MBDragTree tree; |
|
private DefaultTreeModel treeModel; |
|
private JScrollPane treeScroll; |
|
private DefaultMutableTreeNode root; |
|
private String logToKonsole; |
|
|
|
private String sx_db; |
|
|
|
public AdminOrga() { |
|
super(); |
|
|
|
this.setLayout(new BorderLayout()); |
|
|
|
//top panel für Stand |
|
|
|
standDatum = cal.get(Calendar.DATE) + "." |
|
+ (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR); |
|
//aktuelles Datum ist default |
|
|
|
JPanel top = new JPanel(); |
|
standLbl = new JLabel("Stand: " + standDatum); |
|
standLbl.setFont(new Font("Arial", Font.PLAIN, 10)); |
|
JButton bStand = new JButton("anderer Stand"); |
|
bStand.setFont(new Font("Arial", Font.PLAIN, 10)); |
|
bStand.setDefaultCapable(false); |
|
bStand.addActionListener(this); |
|
top.add(standLbl); |
|
top.add(bStand); |
|
JButton bprintanzeige = new JButton("(Druckanzeige)"); |
|
bprintanzeige.addActionListener(this); |
|
bprintanzeige.setFont(new Font("Arial", Font.PLAIN, 10)); |
|
//top.add(bprintanzeige); |
|
this.add(top, "North"); |
|
|
|
//JTree erzeugen |
|
tree = new MBDragTree(); |
|
tree.setRootVisible(false); |
|
tree.setEditable(true); |
|
|
|
treeScroll = new JScrollPane(tree); |
|
this.add(treeScroll, "Center"); |
|
|
|
//Suchen Feld + Buttons |
|
JPanel down = new JPanel(); |
|
down.setLayout(new GridLayout(4, 1)); |
|
|
|
JPanel psuchen = new JPanel(); |
|
suchtext = new JTextField(10); |
|
suchtext.setFont(new Font("Arial", Font.PLAIN, 10)); |
|
psuchen.add(suchtext); |
|
|
|
JButton suchen = new JButton("Suchen"); |
|
suchen.setFont(new Font("Arial", Font.PLAIN, 10)); |
|
suchen.addActionListener(this); |
|
|
|
psuchen.add(suchen); |
|
|
|
down.add(psuchen); |
|
JPanel phinlö = new JPanel(); |
|
JLabel lKnoten = new JLabel("Knoten:"); |
|
phinlö.add(lKnoten); |
|
JButton Nedit = new JButton("bearbeiten"); |
|
Nedit.addActionListener(this); |
|
phinlö.add(Nedit); |
|
JButton Nadd = new JButton("hinzufügen"); |
|
Nadd.addActionListener(this); |
|
phinlö.add(Nadd); |
|
|
|
JButton Ndel = new JButton("löschen"); |
|
Ndel.addActionListener(this); |
|
phinlö.add(Ndel); |
|
|
|
down.add(phinlö); |
|
|
|
JPanel pIO = new JPanel(); |
|
JLabel lorg = new JLabel("Organigramm:"); |
|
pIO.add(lorg); |
|
JButton bspeichern = new JButton("speichern"); |
|
bspeichern.addActionListener(this); |
|
pIO.add(bspeichern); |
|
JButton bneulesen = new JButton("neu einlesen"); |
|
bneulesen.addActionListener(this); |
|
pIO.add(bneulesen); |
|
|
|
JButton bwieder = new JButton("Backup wiederherstellen"); |
|
bwieder.addActionListener(this); |
|
pIO.add(bwieder); |
|
|
|
down.add(pIO); |
|
|
|
this.add("South", down); |
|
|
|
/* |
|
* //pack(); JRootPane rootp = this.getRootPane(); |
|
* rootp.setDefaultButton(null); this.setSize(500, 500); |
|
* this.addWindowListener(new WindowAdapter() { public void |
|
* windowClosing(WindowEvent e) { System.exit(0); }}); |
|
* |
|
* OK.requestFocus(); |
|
*/ |
|
|
|
sx_db = (String) SxDefaults.getPropertyValue("SxDB"); |
|
|
|
} |
|
|
|
public void actionPerformed(ActionEvent event) { |
|
/* |
|
* if (event.getActionCommand().equals("Abbrechen")) { gewaehlt = null; |
|
* setVisible(false); dispose(); } if |
|
* (event.getActionCommand().equals("Ende")) { if (etwasGeändert) //und |
|
* noch nicht abgespeichert { int erg = JOptionPane.showConfirmDialog( |
|
* this, "Vor dem Beenden speichern?", "SuperX DB Admin", |
|
* JOptionPane.YES_NO_CANCEL_OPTION); if (erg == JOptionPane.YES_OPTION) |
|
* speichern(); if (erg == JOptionPane.CANCEL_OPTION) return; } |
|
* this.setVisible(false); dummyframe.show(); |
|
* } |
|
*/ |
|
if (event.getActionCommand().equals("Suchen")) |
|
suchen(); |
|
|
|
if (event.getActionCommand().equals("(Druckanzeige)")) |
|
druckanzeige(); |
|
|
|
if (event.getActionCommand().equals("anderer Stand")) |
|
standÄndern(); |
|
|
|
if (event.getActionCommand().equals("bearbeiten")) |
|
bearbeiten(); |
|
|
|
if (event.getActionCommand().equals("speichern")) |
|
speichern(); |
|
if (event.getActionCommand().equals("neu einlesen")) |
|
readTreeFromDb(); |
|
|
|
if (event.getActionCommand().equals("Backup wiederherstellen")) |
|
backupRestore(); |
|
|
|
if (event.getActionCommand().equals("löschen")) |
|
löschenNode(); |
|
|
|
if (event.getActionCommand().equals("hinzufügen")) |
|
addNode(); |
|
|
|
} |
|
|
|
private void addNode() { |
|
|
|
DefaultMutableTreeNode parentNode = null; |
|
TreePath parentPath = tree.getSelectionPath(); |
|
|
|
if (parentPath == null) { |
|
parentNode = root; |
|
} else { |
|
parentNode = (DefaultMutableTreeNode) (parentPath |
|
.getLastPathComponent()); |
|
} |
|
|
|
//ebene feststellen |
|
int ebene = parentNode.getLevel(); |
|
|
|
OrgaEintrag2 parentEintrag = (OrgaEintrag2) parentNode.getUserObject(); |
|
|
|
java.sql.Date gueltig_seit = new java.sql.Date(1); |
|
gueltig_seit = gueltig_seit.valueOf("2001-10-01"); |
|
java.sql.Date gueltig_bis = new java.sql.Date(1); |
|
gueltig_bis = gueltig_bis.valueOf("2999-01-01"); |
|
OrgaEintrag2 neuerEintrag = new OrgaEintrag2("Neue OrgaEinheit", "", |
|
new String("1"), new Integer(ebene), |
|
parentEintrag.getKeyApnr(), new Integer(1), gueltig_seit, |
|
gueltig_bis, new Integer(0)); |
|
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode( |
|
neuerEintrag); |
|
|
|
OrgaBearbDialog dlg = new OrgaBearbDialog((Frame) this |
|
.getTopLevelAncestor(), childNode, merkungültig, |
|
(DefaultMutableTreeNode) parentNode.getRoot()); |
|
|
|
//wenn nicht Abbrechen, null setzen klappt aus irgend einem Grund nicht |
|
if (childNode != null) { |
|
neuerEintrag = (OrgaEintrag2) childNode.getUserObject(); |
|
if (!neuerEintrag.getName().equals("Neue OrgaEinheit")) { |
|
treeModel.insertNodeInto(childNode, parentNode, parentNode |
|
.getChildCount()); |
|
|
|
// neuen Knoten anzeigen |
|
|
|
tree.scrollPathToVisible(new TreePath(childNode.getPath())); |
|
|
|
setSaveNecessary(true); |
|
} |
|
} |
|
|
|
} |
|
|
|
private void standÄndern() { |
|
if (isSaveNecessary) |
|
{int result = JOptionPane |
|
.showConfirmDialog( |
|
null, |
|
"Bei Auswahl eines anderen Stands gehen eventuell gemachte Änderungen verloren!\n Erst speichern?", |
|
"SuperX DB Admin", JOptionPane.YES_NO_OPTION); |
|
if (result == JOptionPane.OK_OPTION) |
|
speichern(); |
|
} |
|
String neuesStandDatum = JOptionPane |
|
.showInputDialog(this, "Neues Datum eingeben:", "SuperX", |
|
JOptionPane.QUESTION_MESSAGE); |
|
if (neuesStandDatum != null && DateUtils.isValidDate(neuesStandDatum)) //and |
|
// FormOk |
|
|
|
{ |
|
standDatum = neuesStandDatum; |
|
standLbl.setText("Stand: " + standDatum); |
|
readTreeFromDb(); |
|
|
|
this.validate(); |
|
WindowUtils.setWaitCursor(this, false); |
|
} |
|
} |
|
|
|
public void backupAnlegen() { |
|
InfoMessage |
|
.show( |
|
this, |
|
"Achtung!\nBevor Sie das Organigramm bearbeiten sollen Sie auf Datenbankseite eine Sicherung mit\n unload (Informix) bzw. copyto (Postgres) anlegen, damit Sie es bei evtl. Problemen wieder einspielen können.", |
|
"AdminTool"); |
|
WindowUtils.setWaitCursor(this, true); |
|
try { |
|
|
|
DBAccess.executeUpdate("delete from organigramm_bak where 1=1"); |
|
DBAccess |
|
.executeUpdate("insert into organigramm_bak select * from organigramm"); |
|
SuperXDBAdmin.setStatus(SuperXDBAdmin.getStatus() |
|
+ " - backup angelegt!"); |
|
} catch (Exception e) { |
|
JOptionPane.showMessageDialog(null, |
|
"Fehler beim Schreiben in die Datenbank: " + e.toString(), |
|
"SuperX DB Admin", JOptionPane.WARNING_MESSAGE); |
|
e.printStackTrace(); |
|
|
|
} |
|
// WindowUtils.setWaitCursor(this,false); |
|
} |
|
|
|
private void backupRestore() { |
|
boolean OK = true; |
|
try { |
|
|
|
DBAccess.executeUpdate("delete from organigramm where 1=1"); |
|
DBAccess |
|
.executeUpdate("insert into organigramm select * from organigramm_bak"); |
|
|
|
} catch (Exception e) { |
|
JOptionPane.showMessageDialog(null, |
|
"Fehler beim Schreiben in die Datenbank: " + e.toString(), |
|
"SuperX DB Admin", JOptionPane.WARNING_MESSAGE); |
|
e.printStackTrace(); |
|
OK = false; |
|
} |
|
|
|
if (OK) { |
|
readTreeFromDb(); |
|
JOptionPane.showMessageDialog(null, "Backup wieder hergestellt!", |
|
"SuperX DB Admin", JOptionPane.INFORMATION_MESSAGE); |
|
} |
|
|
|
} |
|
|
|
public void bearbeiten() { |
|
|
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree |
|
.getLastSelectedPathComponent(); |
|
|
|
OrgaBearbDialog dlg2 = new OrgaBearbDialog((Frame) this |
|
.getTopLevelAncestor(), node, merkungültig); |
|
DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel(); |
|
|
|
dtm.nodeChanged(node); |
|
setSaveNecessary(true); |
|
} |
|
|
|
public String dateToString(java.sql.Date inp) { |
|
java.text.DateFormat df; |
|
df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM); |
|
// r.b. default Jahr 4-stellig |
|
df.setTimeZone(TimeZone.getDefault()); |
|
df.setLenient(false); |
|
|
|
String help = df.format((java.util.Date) inp); |
|
return help; |
|
} |
|
|
|
public void druckanzeige() { |
|
AdminOrgaPrintRenderer aopr = new AdminOrgaPrintRenderer(); |
|
|
|
tree.setCellRenderer(aopr); |
|
DefaultMutableTreeNode aNode; |
|
DefaultMutableTreeNode theRoot = (DefaultMutableTreeNode) tree |
|
.getModel().getRoot(); |
|
TreePath tp; |
|
for (Enumeration en = theRoot.depthFirstEnumeration(); en |
|
.hasMoreElements();) { |
|
aNode = (DefaultMutableTreeNode) en.nextElement(); |
|
tp = new TreePath(aNode.getPath()); |
|
if (tree.isCollapsed(tp)) |
|
tree.expandPath(tp); |
|
|
|
} |
|
//PrintUtilities.printComponent(treeScroll); |
|
printTree(); |
|
int level; |
|
OrgaEintrag2 x; |
|
String keyApnr; |
|
for (Enumeration en = theRoot.depthFirstEnumeration(); en |
|
.hasMoreElements();) { |
|
aNode = (DefaultMutableTreeNode) en.nextElement(); |
|
if (aNode.getUserObject() instanceof OrgaEintrag2) { |
|
x = (OrgaEintrag2) aNode.getUserObject(); |
|
level = aNode.getLevel(); |
|
for (int i = 1; i <= level; i++) |
|
System.out.print(" "); |
|
keyApnr = x.getKeyApnr() == null ? "" : x.getKeyApnr() |
|
.toString(); |
|
System.out.println(x.getName() + " (" + keyApnr + ")"); |
|
|
|
} |
|
|
|
} |
|
} |
|
|
|
public static java.sql.Date getDate(String value) { |
|
|
|
DateFormat df; |
|
java.util.Date date; |
|
java.sql.Date sql_date; |
|
Calendar calendar; |
|
|
|
df = DateFormat.getDateInstance(DateFormat.MEDIUM); |
|
// r.b. default Jahr 4-stellig |
|
df.setTimeZone(TimeZone.getDefault()); |
|
df.setLenient(false); |
|
|
|
try { |
|
if (value.endsWith("00")) { |
|
switch (value.charAt(value.length() - 3)) { |
|
case '-': |
|
case '.': |
|
case '/': |
|
value = value.substring(0, value.length() - 2) + "2000"; |
|
break; |
|
} |
|
} |
|
|
|
date = df.parse(value); |
|
calendar = Calendar.getInstance(); |
|
calendar.setTime(date); |
|
int year = calendar.get(Calendar.YEAR); |
|
if (year >= 0 && year < 30) |
|
calendar.add(Calendar.YEAR, 2000); |
|
if (year >= 30 && year < 100) |
|
calendar.add(Calendar.YEAR, 1900); |
|
|
|
sql_date = new java.sql.Date(calendar.getTime().getTime()); |
|
return sql_date; |
|
} catch (ParseException e_parse) { |
|
// System.err.println ("SxDate: ERROR: parse: Date != " + value); |
|
} catch (IllegalArgumentException e_arg) { |
|
// System.err.println ("SxDate: ERROR: arg: Date != " + value); |
|
} |
|
|
|
return null; |
|
} |
|
|
|
public OrgaEintrag2 getGewaehlt() { |
|
return gewaehlt; |
|
} |
|
|
|
private void insertnodes(String theParent, DefaultMutableTreeNode aktNode) { |
|
//rekursive Methode, die Institutionen in den Baum einträgt |
|
Vector result = new Vector(); |
|
result = mySelect(theParent); |
|
String eintrag_key; |
|
for (Enumeration el = result.elements(); el.hasMoreElements();) { |
|
einEintrag = (OrgaEintrag2) el.nextElement(); |
|
DefaultMutableTreeNode neuNode = new DefaultMutableTreeNode( |
|
einEintrag); |
|
aktNode.add(neuNode); |
|
|
|
eintrag_key = einEintrag.getKeyApnr(); |
|
//nervig null-Werte abfangen |
|
if (eintrag_key != null & theParent == null) |
|
insertnodes(einEintrag.getKeyApnr(), neuNode); |
|
if (eintrag_key == null & theParent == null) |
|
System.out |
|
.println("Achtung! Element hat fälschlicherweise sich selbst als parent:" |
|
+ einEintrag.getName()); |
|
|
|
if (eintrag_key != null && theParent != null) |
|
if (!theParent.equals(eintrag_key)) |
|
insertnodes(einEintrag.getKeyApnr(), neuNode); |
|
else |
|
System.out |
|
.println("Achtung! Element hat fälschlicherweise sich selbst als parent:" |
|
+ einEintrag.getName() |
|
+ " key_apnr:" |
|
+ einEintrag.getKeyApnr()); |
|
} |
|
|
|
} |
|
|
|
public int getChildCount(DefaultMutableTreeNode node) { |
|
int zähler = 0; |
|
DefaultMutableTreeNode aNode; |
|
for (Enumeration en = node.depthFirstEnumeration(); en |
|
.hasMoreElements();) { |
|
aNode = (DefaultMutableTreeNode) en.nextElement(); |
|
zähler++; |
|
} |
|
return zähler - 1; |
|
|
|
} |
|
|
|
public void log(String was) { |
|
System.out.println(was); |
|
//ggfs. in Datei schreiben |
|
} |
|
|
|
private void löschenNode() { |
|
|
|
TreePath currentSelection = tree.getSelectionPath(); |
|
if (currentSelection != null) { |
|
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection |
|
.getLastPathComponent()); |
|
MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent()); |
|
if (parent != null) { |
|
treeModel.removeNodeFromParent(currentNode); |
|
setSaveNecessary(true); |
|
return; |
|
} |
|
} |
|
} |
|
|
|
private Vector mySelect(String pruefparent) { |
|
//liefert alle Institute mit Parent =pruefparent zurück |
|
Vector result = new Vector(); |
|
String eintrag_parent; |
|
for (Enumeration el = orga.elements(); el.hasMoreElements();) { |
|
einEintrag = (OrgaEintrag2) el.nextElement(); |
|
eintrag_parent = einEintrag.getParent(); |
|
if (eintrag_parent == null && pruefparent == null) |
|
result.addElement(einEintrag); |
|
if (eintrag_parent != null && pruefparent != null) |
|
if (eintrag_parent.equals(pruefparent)) |
|
result.addElement(einEintrag); |
|
|
|
} |
|
return result; |
|
} |
|
|
|
public int print(Graphics g, PageFormat pageFormat, int pageIndex) |
|
throws PrinterException { |
|
/** |
|
* r.b. Hinweis: Parameter der Druckfunktion 'print': - Graphics = |
|
* Graphikkontext (casten -> Graphics2D) - PageFormat = Seitenformat - |
|
* int pageIndex = Seitenindex Allgemeine Vorgehensweise beim Drucken: - |
|
* Seiten groesse bestimmen: (pageFormat.getImageableHeight() |
|
* (pageFormat.getImageableWidth() - Skalierungsfaktor bestimmen |
|
* (optional) - mit 'translate' Anfangspunkt jeder Druckoperation setzen - |
|
* (optional) mit 'setClip' begrenzendes Rechteck setzen |
|
*/ |
|
Graphics2D g2 = (Graphics2D) g; |
|
|
|
g2.setColor(Color.black); |
|
|
|
int fontHeight = g2.getFontMetrics().getHeight(); |
|
int fontDesent = g2.getFontMetrics().getDescent(); |
|
|
|
//leave room for page number |
|
double pageHeight = pageFormat.getImageableHeight() - fontHeight |
|
- fontDesent; |
|
double pageWidth = pageFormat.getImageableWidth(); |
|
|
|
double treeWidth = (double) tree.WIDTH; |
|
|
|
double scale = 1; |
|
|
|
if (treeWidth >= pageWidth) { |
|
scale = pageWidth / (treeWidth); |
|
} |
|
double treeWidthOnPage = treeWidth * scale; |
|
|
|
double oneRowHeight = (tree.getRowHeight()) * scale; |
|
int numRowsOnAPage = (int) ((pageHeight - 90) / oneRowHeight); |
|
double pageHeightForTree = oneRowHeight * numRowsOnAPage; |
|
int totalNumPages = (int) Math.ceil(((double) tree.getRowCount()) |
|
/ numRowsOnAPage); |
|
|
|
// System.err.println ("scale: " + scale); |
|
// System.err.println ("numRowsOnAPage: " + numRowsOnAPage); |
|
// System.err.println ("pageHeightForTable: " + pageHeightForTable); |
|
|
|
if (pageIndex >= totalNumPages) { |
|
return Printable.NO_SUCH_PAGE; |
|
} |
|
|
|
// scale fontsize |
|
Font font = g2.getFont(); |
|
Font pr_font = new Font(font.getFontName(), font.getStyle(), |
|
(int) (font.getSize() * scale)); |
|
Font b_font = new Font(font.getFontName(), Font.BOLD, (int) (font |
|
.getSize() * scale)); |
|
|
|
g2.setFont(pr_font); |
|
|
|
//bottom center |
|
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); |
|
int footWidth = g2.getFontMetrics().stringWidth("Seite #"); |
|
g2.setFont(b_font); |
|
g2.drawString("SuperX", (int) 0f, (int) (pageHeight + fontHeight)); |
|
|
|
g2.setFont(pr_font); |
|
g2.drawString("Seite " + (pageIndex + 1), |
|
(int) (pageWidth / 2 - footWidth / 2), |
|
(int) (pageHeight + fontHeight)); |
|
|
|
/* |
|
* double dateWidthOnPage = (double) g2.getFontMetrics().stringWidth |
|
* (updStatus.getText()) + 2; g2.drawString(updStatus.getText(), (int) |
|
* (pageWidth - dateWidthOnPage), (int) (pageHeight + fontHeight)); |
|
*/ |
|
g2.setFont(pr_font); |
|
|
|
g2.translate(0f, 90); |
|
g2.translate(0f, -pageIndex * pageHeightForTree); |
|
|
|
//If this piece of the table is smaller than the size available, |
|
//clip to the appropriate bounds. |
|
if (pageIndex + 1 == totalNumPages) { |
|
int lastRowPrinted = numRowsOnAPage * pageIndex; |
|
int numRowsLeft = tree.getRowCount() - lastRowPrinted; |
|
g2.setClip(0, (int) (pageHeightForTree * pageIndex), (int) Math |
|
.ceil(treeWidthOnPage), (int) Math.ceil(pageHeightForTree)); |
|
} |
|
//else clip to the entire area available. |
|
else { |
|
g2.setClip(0, (int) (pageHeightForTree * pageIndex), (int) Math |
|
.ceil(treeWidthOnPage), (int) Math.ceil(pageHeightForTree)); |
|
} |
|
|
|
g2.scale(scale, scale); |
|
|
|
try { |
|
tree.print(g2); |
|
} catch (IllegalArgumentException t_ex) { |
|
System.err.println(t_ex); |
|
} |
|
|
|
// Ueberschriften drucken |
|
/* |
|
* g2.scale(1/scale,1/scale); g2.translate(-paramWidth, -30); |
|
* g2.setClip(0, 0, (int) Math.ceil(rowheaderWidthOnPage + |
|
* treeWidthOnPage), (int)Math.ceil(headerHeightOnPage)); |
|
* g2.scale(scale,scale); |
|
* |
|
* g2.setFont(h_font); headLine.print(g2);//print headline at top |
|
*/ |
|
return Printable.PAGE_EXISTS; |
|
|
|
} |
|
|
|
public void printTree() { |
|
PrinterJob pj = PrinterJob.getPrinterJob(); |
|
|
|
PageFormat defaultFormat = pj.defaultPage(); |
|
// Dialog zur Auswahl des Papierformats |
|
PageFormat pageFormat = pj.pageDialog(defaultFormat); |
|
|
|
if (pageFormat.equals(defaultFormat)) |
|
return; |
|
|
|
// A4 festvorgeben. |
|
// Papiergroesse DIN A4, Rand: O + U: 1/2 Inch, L + R: 1/2 Inch |
|
// alle angaben in 1/72 Inch |
|
|
|
Paper a4 = new Paper(); |
|
a4.setSize(21 / 2.54 * 72, 29.7 / 2.54 * 72); |
|
a4.setImageableArea(36, 36, (21 / 2.54 * 72) - 72, |
|
(29.7 / 2.54 * 72) - 72); |
|
|
|
pageFormat.setPaper(a4); |
|
defaultFormat = pageFormat; |
|
// pageFormat.setOrientation (PageFormat.LANDSCAPE); |
|
|
|
// System.err.println ("p_width: " + pageFormat.getImageableWidth()); |
|
// System.err.println ("p_height: " + pageFormat.getImageableHeight()); |
|
|
|
//MB printing |
|
//tmp_druck=null; |
|
//ggfs_nurMarkierterBereich(pj,pageFormat); |
|
|
|
//if (tmp_druck==null) |
|
pj.setPrintable(this, pageFormat); |
|
//else |
|
// pj.setPrintable(tmp_drucktab, pageFormat); |
|
|
|
if (!pj.printDialog()) |
|
return; |
|
|
|
try { |
|
|
|
pj.print(); |
|
} catch (Exception PrintException) { |
|
System.err.println(PrintException); |
|
PrintException.printStackTrace(); |
|
} |
|
|
|
} |
|
|
|
public void prüfeDifferenzen(DefaultMutableTreeNode root) { |
|
Vector problemfälle; |
|
problemfälle = (Vector) orga.clone(); |
|
DefaultMutableTreeNode aNode; |
|
OrgaEintrag2 x; |
|
String name; |
|
Integer key_apnr; |
|
java.sql.Date gueltig_seit, gueltig_bis; |
|
|
|
//Problemfälle enthält zu Anfang alle eingelesenen, gültigen Einträge |
|
//diejenigen, die in Baum vorkommen werden ausgelöscht |
|
for (Enumeration en = root.depthFirstEnumeration(); en |
|
.hasMoreElements();) { |
|
aNode = (DefaultMutableTreeNode) en.nextElement(); |
|
x = (OrgaEintrag2) aNode.getUserObject(); |
|
problemfälle.remove(x); |
|
} |
|
|
|
if (problemfälle.size() > 0) { |
|
JOptionPane |
|
.showMessageDialog( |
|
null, |
|
"Achtung!\nEs gibt " |
|
+ problemfälle.size() |
|
+ " gültige Einträge, die nicht in die Baumstruktur passten!\n(Siehe Knoten Fehler)", |
|
"SuperX Admin", JOptionPane.WARNING_MESSAGE); |
|
log(problemfälle.size() |
|
+ " gültige Einträge, die nicht in die Baumstruktur passten:"); |
|
|
|
DefaultMutableTreeNode fehlernode = new DefaultMutableTreeNode( |
|
"Fehler"); |
|
|
|
for (Enumeration el = problemfälle.elements(); el.hasMoreElements();) { |
|
x = (OrgaEintrag2) el.nextElement(); |
|
fehlernode.add(new DefaultMutableTreeNode(x)); |
|
log(x.getKeyApnr() + " - " + x.getName() + "parent (" |
|
+ x.getParent() + ")"); |
|
|
|
} |
|
root.add(fehlernode); |
|
} |
|
} |
|
|
|
public void setGewaehlt(OrgaEintrag2 was) { |
|
gewaehlt = was; |
|
} |
|
|
|
public void speichern() { |
|
Enumeration en; |
|
int i = 0; |
|
String ins = null; |
|
StringBuffer sql = new StringBuffer( |
|
"create table tmp_organigramm" |
|
+ "(tid serial not null ,key_apnr char(10),parent char(10),drucktext char(150),name char(150),ebene integer,lehre smallint,gueltig_seit date not null ,gueltig_bis date not null ,orgstruktur integer);"); |
|
String wo = null; |
|
WindowUtils.setWaitCursor(this, true); |
|
|
|
// MBProgressBar progress=new MBProgressBar(10); |
|
try { |
|
DefaultMutableTreeNode lroot = (DefaultMutableTreeNode) tree |
|
.getModel().getRoot(); |
|
DefaultMutableTreeNode aNode; |
|
OrgaEintrag2 x; |
|
DBAccess.executeUpdate("delete from organigramm where 1=1"); |
|
|
|
en = merkungültig.elements(); |
|
|
|
String pruf; |
|
while (en.hasMoreElements()) { |
|
x = (OrgaEintrag2) en.nextElement(); |
|
|
|
i++; |
|
sql.append(getInsertCommand(x)); |
|
//progress.inc(); |
|
} |
|
|
|
try { |
|
|
|
DBAccess.executeUpdateAll(sql.toString()); |
|
} catch (SQLException e) { |
|
// System.out.println(sql); |
|
System.out |
|
.println("Fehler beim Schreiben eines Orga-Eintrags in die Datenbank:\n " |
|
+ e.toString()); |
|
throw new SQLException(); |
|
//System.out.println(ins); |
|
} catch (Exception e) { |
|
//System.out.println(sql); |
|
System.out |
|
.println("Fehler beim Schreiben eines Orga-Eintrags in die Datenbank:\n " |
|
+ e.toString()); |
|
throw new Exception(); |
|
} |
|
TreePath tp; |
|
DefaultMutableTreeNode vorher; |
|
aNode = null; |
|
sql = new StringBuffer(); |
|
/* |
|
* "create temp table tmp_organigramm" + "(" + "tid serial not null ," + |
|
* "key_apnr char(10)," + "parent char(10)," + "drucktext |
|
* char(150)," + "name char(150)," + "ebene integer," + "lehre |
|
* smallint," + "gueltig_seit date not null ," + "gueltig_bis date |
|
* not null ," + "orgstruktur integer" + ");"); |
|
*/ |
|
for (en = lroot.depthFirstEnumeration(); en.hasMoreElements();) { |
|
vorher = aNode; |
|
aNode = (DefaultMutableTreeNode) en.nextElement(); |
|
if (!aNode.toString().equals("Fehler")) { |
|
x = (OrgaEintrag2) aNode.getUserObject(); |
|
if (!x.getName().equals("Hochschule")) { |
|
tp = new TreePath(aNode.getUserObjectPath()); |
|
// System.out.println(tp.toString()); |
|
pruf = x.getKeyApnr(); |
|
// if (pruf == 111110) |
|
// System.out.println(x.getName() + " - " + |
|
// x.getParent()); |
|
} |
|
|
|
if (x.getKeyApnr() != null) { |
|
sql.append(getInsertCommand(x)); |
|
} |
|
|
|
i++; |
|
//progress.inc(); |
|
} //Wenn Fehler |
|
} //Schleife |
|
try { |
|
sql |
|
.append("insert into organigramm select * from tmp_organigramm; drop table tmp_organigramm;"); |
|
DBAccess.executeUpdateAll(sql.toString()); |
|
} catch (SQLException e) { |
|
//System.out.println(ins); |
|
System.out |
|
.println("Fehler beim Schreiben eines Problemfalls in die Datenbank:\n " |
|
+ e.toString()); |
|
throw new SQLException(); |
|
} catch (Exception e) { |
|
//System.out.println(ins); |
|
System.out |
|
.println("Fehler beim Schreiben eines Problemfalls in die Datenbank:\n " |
|
+ e.toString()); |
|
throw new Exception(); |
|
} |
|
|
|
SuperXDBAdmin.setStatus(i - 1 |
|
+ " Einträge ins Organigramm geschrieben"); |
|
|
|
} catch (SQLException e) { |
|
JOptionPane.showMessageDialog(null, |
|
"Fehler beim Schreiben in die Datenbank (siehe Java-Konsole):\n " |
|
+ e.toString(), "SuperX DB Admin", |
|
JOptionPane.WARNING_MESSAGE); |
|
//System.out.println(wo + ": " + ins); |
|
//e.printStackTrace(); |
|
|
|
} catch (Exception e) { |
|
JOptionPane.showMessageDialog(null, |
|
"Allgemeiner Fehler beim Schreiben in die Datenbank (siehe Java-Konsole):\n " |
|
+ e.toString(), "SuperX DB Admin", |
|
JOptionPane.WARNING_MESSAGE); |
|
//System.out.println(wo + ": " + ins); |
|
//e.printStackTrace(); |
|
} |
|
setSaveNecessary(false); |
|
WindowUtils.setWaitCursor(this, false); |
|
|
|
|
|
//progress.hide(); |
|
//progress.dispose(); |
|
|
|
} |
|
|
|
public boolean isSaveNecessary() { |
|
return isSaveNecessary; |
|
} |
|
|
|
private void setSaveNecessary(boolean mussNochSpeichern) { |
|
this.isSaveNecessary = mussNochSpeichern; |
|
} |
|
|
|
private String getInsertCommand(OrgaEintrag2 x) { |
|
String ins, name, drucktext; |
|
name = x.getName(); |
|
if (name != null) |
|
name = name.replaceAll("'", "''"); |
|
drucktext = x.getName(); |
|
if (drucktext != null) |
|
drucktext = drucktext.replaceAll("'", "''"); |
|
if (sx_db.equals("Postgres")) { |
|
ins = "insert into tmp_organigramm (name,drucktext,key_apnr,ebene,parent,lehre,orgstruktur,gueltig_seit,gueltig_bis) values ('" |
|
+ name |
|
+ "','" |
|
+ drucktext |
|
+ "','" |
|
+ x.getKeyApnr() |
|
+ "'," |
|
+ x.getEbene() |
|
+ ",'" |
|
+ x.getParent() |
|
+ "'," |
|
+ x.getLehre() |
|
+ "," |
|
+ x.getOrgstruktur() |
|
+ ",date_val('" |
|
+ SxUtils.GetValueAsString(x.getGueltigSeit()) |
|
+ "'),date_val('" |
|
+ SxUtils.GetValueAsString(x.getGueltigBis()) + "')); \n"; |
|
} else { |
|
//INformix: |
|
ins = new String( |
|
"insert into tmp_organigramm (name,drucktext,key_apnr,ebene,parent,lehre,orgstruktur,gueltig_seit,gueltig_bis) values ('" |
|
+ name |
|
+ "','" |
|
+ drucktext |
|
+ "','" |
|
+ x.getKeyApnr() |
|
+ "'," |
|
+ x.getEbene() |
|
+ ",'" |
|
+ x.getParent() |
|
+ "'," |
|
+ x.getLehre() |
|
+ "," |
|
+ x.getOrgstruktur() |
|
+ ",date(\"" |
|
+ SxUtils.GetValueAsString(x.getGueltigSeit()) |
|
+ "\"),date(\"" |
|
+ SxUtils.GetValueAsString(x.getGueltigBis()) |
|
+ "\"));\n "); |
|
} |
|
return ins; |
|
} |
|
|
|
private void suchen() { |
|
String such = suchtext.getText().toLowerCase(); |
|
String teststring; |
|
TreePath tp; |
|
boolean gefunden = false; |
|
DefaultMutableTreeNode test, ausgesucht; |
|
ausgesucht = null; |
|
Vector treffer = new Vector(); |
|
Vector treffernodes = new Vector(); |
|
|
|
if (such.length() < 1) { |
|
JOptionPane.showMessageDialog(null, "Bitte Suchbegriff eingeben!", |
|
"SuperX", 1); |
|
return; |
|
} |
|
such = such.toLowerCase(); |
|
//JOptionPane.showMessageDialog(null, "neu","SuperX",1); |
|
//alle Knoten durchlaufen |
|
root = (DefaultMutableTreeNode) tree.getModel().getRoot(); |
|
Enumeration en = root.breadthFirstEnumeration(); |
|
while (en.hasMoreElements()) { |
|
test = (DefaultMutableTreeNode) en.nextElement(); |
|
teststring = test.toString().toLowerCase(); |
|
|
|
if (teststring.indexOf(such) > -1) { |
|
//wenn suchString im aktuellen Knoten vorhanden |
|
//K |
|
treffer.addElement(test.toString()); |
|
treffernodes.addElement(test); |
|
} |
|
} |
|
if (treffer.size() > 0) { |
|
if (treffer.size() > 1) { |
|
//mehr als ein Treffer -> Auswahldialog |
|
String[] trefferstring = new String[treffer.size()]; |
|
treffer.copyInto(trefferstring); |
|
|
|
String selectedValue = (String) JOptionPane.showInputDialog( |
|
null, "Mehrere Treffer!\nBitte auswählen...", "SuperX", |
|
JOptionPane.INFORMATION_MESSAGE, null, trefferstring, |
|
trefferstring[0]); |
|
if (selectedValue == null) |
|
return; |
|
gefunden = false; |
|
//gewählten Knoten nach Name in Treffernodes-Vektor suchen |
|
en = treffernodes.elements(); |
|
while (en.hasMoreElements() && gefunden == false) { |
|
test = (DefaultMutableTreeNode) en.nextElement(); |
|
if (test.toString().equals(selectedValue)) { |
|
ausgesucht = test; |
|
gefunden = true; |
|
} |
|
} |
|
} |
|
if (ausgesucht == null) //bei treffer.size=1 |
|
ausgesucht = (DefaultMutableTreeNode) treffernodes.elementAt(0); |
|
// collapse all brances + Selection anwählen |
|
tree.clearSelection(); |
|
en = root.depthFirstEnumeration(); |
|
while (en.hasMoreElements()) { |
|
test = (DefaultMutableTreeNode) en.nextElement(); |
|
tp = new TreePath(test.getPath()); |
|
if (tree.isExpanded(tp)) |
|
tree.collapsePath(tp); |
|
} |
|
|
|
tp = new TreePath(ausgesucht.getPath()); |
|
tree.addSelectionPath(tp); |
|
//tree.makeVisible(tp); |
|
|
|
// tree.validate(); |
|
// tree.repaint(); |
|
} else |
|
JOptionPane.showMessageDialog(null, "Keine Treffer", "SuperX", 1); |
|
} |
|
|
|
void readTreeFromDb() { |
|
|
|
try { |
|
DefaultMutableTreeNode eb1, eb2, eb3, eb4, eb5, eb6; |
|
|
|
int i = 0; |
|
orga = new Vector(); |
|
merkungültig = new Vector(); |
|
String name, drucktext; |
|
Integer ebene, lehre, orgstruktur; |
|
String key_apnr, parent; |
|
java.sql.Date gueltig_seit = null, gueltig_bis = null; |
|
|
|
String query = "SELECT name,drucktext,key_apnr,ebene,parent,lehre,gueltig_seit,gueltig_bis,orgstruktur FROM organigramm order by name"; |
|
|
|
int validCount = 0; |
|
int errorCount = 0; |
|
|
|
Vector erg = DBAccess.executeQuery(query); |
|
entryCount = 0; |
|
java.sql.Timestamp mytimestamp; |
|
long mytimestampwert; |
|
java.sql.Date StandDate = getDate(standDatum); |
|
// new java.sql.Date(cal.getTime().getTime()); |
|
|
|
Vector help; |
|
for (Enumeration el = erg.elements(); el.hasMoreElements();) { |
|
help = (Vector) el.nextElement(); |
|
|
|
name = help.get(0).toString(); |
|
drucktext = help.get(1).toString(); |
|
key_apnr = (String) help.get(2); |
|
ebene = (Integer) help.get(3); |
|
parent = (String) help.get(4); |
|
Object helpx = help.get(5); |
|
|
|
if (key_apnr.equals(parent)) { |
|
errorCount++; |
|
log("Eintrag hat sich selbst als parent: " + key_apnr); |
|
} |
|
|
|
lehre = new Integer(helpx.toString()); |
|
orgstruktur = (Integer) help.get(8); |
|
|
|
//System.out.println(rsmd.getColumnClassName(4)); |
|
//für Access |
|
/* |
|
* if (DB.equals("access")) { mytimestamp = (java.sql.Timestamp) |
|
* rs1.getObject("gueltig_seit"); mytimestampwert = |
|
* mytimestamp.getTime(); gueltig_seit = new |
|
* java.sql.Date(mytimestampwert); |
|
* |
|
* mytimestamp = (java.sql.Timestamp) |
|
* rs1.getObject("gueltig_bis"); mytimestampwert = |
|
* mytimestamp.getTime(); gueltig_bis = new |
|
* java.sql.Date(mytimestampwert); } if (DB.equals("informix")) { |
|
*/ |
|
gueltig_seit = (java.sql.Date) help.get(6); |
|
gueltig_bis = (java.sql.Date) help.get(7); |
|
|
|
einEintrag = new OrgaEintrag2(name, drucktext, key_apnr, ebene, |
|
parent, lehre, gueltig_seit, gueltig_bis, orgstruktur); |
|
|
|
if ((gueltig_seit.equals(StandDate) || gueltig_seit |
|
.before(StandDate)) |
|
&& (gueltig_bis.equals(StandDate) || gueltig_bis |
|
.after(StandDate))) { |
|
orga.addElement(einEintrag); |
|
validCount++; |
|
} else |
|
merkungültig.addElement(einEintrag); |
|
|
|
entryCount++; |
|
} |
|
if (errorCount > 0) |
|
JOptionPane.showMessageDialog(null, "Achtung!\n" + errorCount |
|
+ " Einträge haben sich selbst als parent!", |
|
"SuperX Admin", JOptionPane.WARNING_MESSAGE); |
|
|
|
OrgaEintrag2 hochschule = new OrgaEintrag2("Hochschule", null, |
|
null, null, null, null, null, null, null); |
|
DefaultMutableTreeNode root = new DefaultMutableTreeNode(hochschule); |
|
|
|
//hier werden über eine rekursive Methode alle |
|
//Institute in den Baum eingetragen |
|
insertnodes("", root); |
|
//bisher insertnodes(null,root) wegen Problemen bei Übergabe im |
|
// Servlet von NULL Werten |
|
|
|
//Konsistenz prüfen |
|
int treeNodeCount = getChildCount(root); |
|
if (validCount != treeNodeCount) |
|
prüfeDifferenzen(root); |
|
|
|
//Baum erzeugen |
|
treeModel = new DefaultTreeModel(root); |
|
tree.setModel(treeModel); |
|
//root Hochschule sieht man sowieso nicht |
|
//first child GMU Duisburg muss aufgeklappt sein |
|
//da Mouselistener Doubleclick umfunktioniert |
|
DefaultMutableTreeNode ebene1 = (DefaultMutableTreeNode) root |
|
.getChildAt(0); |
|
tree.expandPath(new TreePath(ebene1.getPath())); |
|
|
|
//Einfachselektion |
|
tree.getSelectionModel().setSelectionMode( |
|
TreeSelectionModel.SINGLE_TREE_SELECTION); |
|
|
|
//Markierungsrandfarbe grau nicht gelb |
|
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); |
|
renderer.setBorderSelectionColor(java.awt.Color.darkGray); |
|
// ImageIcon leaf = new ImageIcon("h:\\mb\\leaf.gif"); |
|
// if (leaf != null) |
|
// renderer.setLeafIcon(leaf); |
|
tree.setCellRenderer(renderer); |
|
|
|
//TreeSelectionListener hinzufügen |
|
AdminOrgaSelList adosl = new AdminOrgaSelList(this, tree); |
|
tree.addTreeSelectionListener(adosl); |
|
|
|
tree.addKeyListener(this); |
|
AdminOrgaMouseListener ml = new AdminOrgaMouseListener(this, tree, |
|
merkungültig); |
|
tree.addMouseListener(ml); |
|
|
|
SuperXDBAdmin.setStatus(entryCount + " OrgaEinträge eingelesen"); |
|
|
|
} catch (Exception e) { |
|
JOptionPane.showMessageDialog(null, |
|
"Fehler beim Lesen aus der Datenbank: " + e.toString(), |
|
"SuperX DB Admin", JOptionPane.WARNING_MESSAGE); |
|
e.printStackTrace(); |
|
|
|
} |
|
WindowUtils.setWaitCursor(this,false); |
|
this.validate(); |
|
} |
|
|
|
public void keyPressed(KeyEvent event) { |
|
if (event.getKeyCode() == KeyEvent.VK_DELETE) |
|
löschenNode(); |
|
|
|
} |
|
|
|
public void keyReleased(KeyEvent event) { |
|
} |
|
|
|
public void keyTyped(KeyEvent event) { |
|
if (event.getKeyCode() == KeyEvent.VK_DELETE) |
|
löschenNode(); |
|
} |
|
|
|
} |