001: /*
002: * GenericRowMonitor.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.components;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016: import workbench.interfaces.StatusBar;
017: import workbench.log.LogMgr;
018: import workbench.resource.ResourceMgr;
019: import workbench.storage.RowActionMonitor;
020: import workbench.util.NumberStringCache;
021:
022: /**
023: *
024: * @author support@sql-workbench.net
025: */
026: public class GenericRowMonitor implements RowActionMonitor {
027: private StatusBar statusBar;
028: private String updateMsg;
029: private String currentMonitorObject;
030: private int monitorType;
031: private String objectMsg = ResourceMgr
032: .getString("MsgProcessObject")
033: + " ";
034: private Map<String, TypeEntry> typeStack = new HashMap<String, TypeEntry>();
035:
036: public GenericRowMonitor(StatusBar status) {
037: this .statusBar = status;
038: }
039:
040: public int getMonitorType() {
041: return this .monitorType;
042: }
043:
044: public void setMonitorType(int type) {
045: this .monitorType = type;
046: switch (type) {
047: case RowActionMonitor.MONITOR_INSERT:
048: this .updateMsg = ResourceMgr.getString("MsgImportingRow")
049: + " ";
050: break;
051: case RowActionMonitor.MONITOR_UPDATE:
052: this .updateMsg = ResourceMgr.getString("MsgUpdatingRow")
053: + " ";
054: break;
055: case RowActionMonitor.MONITOR_LOAD:
056: this .updateMsg = ResourceMgr.getString("MsgLoadingRow")
057: + " ";
058: break;
059: case RowActionMonitor.MONITOR_EXPORT:
060: this .updateMsg = ResourceMgr.getString("MsgWritingRow")
061: + " ";
062: break;
063: case RowActionMonitor.MONITOR_COPY:
064: this .updateMsg = ResourceMgr.getString("MsgCopyingRow")
065: + " ";
066: break;
067: case RowActionMonitor.MONITOR_PROCESS_TABLE:
068: this .updateMsg = ResourceMgr.getString("MsgProcessTable")
069: + " ";
070: break;
071: case RowActionMonitor.MONITOR_PROCESS:
072: this .updateMsg = ResourceMgr.getString("MsgProcessObject")
073: + " ";
074: break;
075: case RowActionMonitor.MONITOR_PLAIN:
076: this .updateMsg = null;
077: break;
078: default:
079: LogMgr.logWarning("GenericRowMonitor.setMonitorType()",
080: "Invalid monitor type " + type + " specified!");
081: this .monitorType = RowActionMonitor.MONITOR_PLAIN;
082: this .updateMsg = null;
083: }
084: }
085:
086: public void setCurrentObject(String name, long number, long total) {
087: if (this .monitorType == RowActionMonitor.MONITOR_PLAIN) {
088: statusBar.setStatusMessage(name);
089: } else {
090: this .currentMonitorObject = name;
091: StringBuilder msg = new StringBuilder(40);
092: if (objectMsg != null)
093: msg.append(objectMsg);
094: msg.append(name);
095: if (number > 0) {
096: msg.append(" (");
097: msg.append(number);
098: if (total > 0) {
099: msg.append('/');
100: msg.append(total);
101: }
102: msg.append(')');
103: }
104: statusBar.setStatusMessage(msg.toString());
105: }
106: }
107:
108: public void setCurrentRow(long currentRow, long totalRows) {
109: StringBuilder msg = new StringBuilder(40);
110: if (this .updateMsg == null) {
111: msg.append(objectMsg);
112: msg.append(this .currentMonitorObject);
113: msg.append(" (");
114: } else {
115: msg.append(this .updateMsg);
116: }
117: msg.append(NumberStringCache.getNumberString(currentRow));
118: if (totalRows > 0) {
119: msg.append('/');
120: msg.append(NumberStringCache.getNumberString(totalRows));
121: }
122: if (this .updateMsg == null)
123: msg.append(')');
124: statusBar.setStatusMessage(msg.toString());
125: }
126:
127: public void jobFinished() {
128: statusBar.clearStatusMessage();
129: }
130:
131: public void saveCurrentType(String type) {
132: TypeEntry entry = new TypeEntry();
133: entry.msg = this .updateMsg;
134: entry.type = this .monitorType;
135: entry.obj = this .currentMonitorObject;
136: this .typeStack.put(type, entry);
137: }
138:
139: public void restoreType(String type) {
140: TypeEntry entry = typeStack.get(type);
141: if (entry == null)
142: return;
143: this .updateMsg = entry.msg;
144: this .currentMonitorObject = entry.obj;
145: this .monitorType = entry.type;
146: }
147:
148: }
149:
150: class TypeEntry {
151: int type;
152: String msg;
153: String obj;
154:
155: public TypeEntry() {
156: }
157: }
|