Einbinden eines Auswahldialogs für Stempelannotationen
Einbinden eines Auswahldialogs für Stempelannotationen
Es wird oft der Wunsch geäußert, beim Anlegen einer Stempelannotation einen Auswahldialog mit vordefinierten Stempeln zur Anzeige zu bringen. Diese Anforderung kann nicht über das Annotationsprofil bewerkstelligt werden, hierzu muss eine eigene Command- / Tool-Implementierung erstellt werden.
Erstellen der Command-Implementation
Die Commands für das Anlegen der Annotationen werden in der com.levigo.jadice.appbase.AnnotationProfileAwareToolBar-Klasse aus dem Annotationsprofil erstellt. Die AnnotationProfileAwareToolBar-Implementation muss angepasst werden, der Quellcode ist in der lib/appbase/jadice-appbase-x.x.x.x-sources.jar Datei unter com/levigo/jadice/appbase/AnnotationProfileAwareToolBar.java zu finden. Hier muss in der Methode populateComponent(...) die Zeile
final AnnotationModeCommand cmd = new AnnotationModeCommand();
durch
AnnotationModeCommand cmd;
// hier den Stempel-Annotyp erkennen, der für die Dialogauswahl vorgsehen ist
if (typeName.equals("Stamp")) {
cmd = new AnnotationModeCommand() {
private final boolean toolRegistered = false;
@Override
protected void execute() {
if (!toolRegistered) {
// Tool-Implementation registrieren / aktivieren
getPageView().getToolManager().register(com.levigo.jadice.appbase.StampAnnoTool.class, true);
getPageView().getToolManager().activate(com.levigo.jadice.appbase.StampAnnoTool.class);
}
// Tool auf aktiv setzen
getPageView().getToolManager().getTool(com.levigo.jadice.appbase.StampAnnoTool.class).setToolToActivated();
// Tool auf exklusiv Modus setzen, andere Tools sind nicht aktiv
getPageView().getToolManager().setExclusive(com.levigo.jadice.appbase.StampAnnoTool.class);
}
};
} else {
cmd = new AnnotationModeCommand();
}
Codebeispiel für die StampAnnoTool-Implementation
package com.levigo.jadice.appbase;
import java.awt.Cursor;
import javax.swing.UIManager;
import com.levigo.jadice.annotation.AnnotationPageSegment;
import com.levigo.jadice.annotation.Annotations;
import com.levigo.jadice.annotation.StampAnnotation;
import com.levigo.jadice.annotation.profiles.AnnotationFactory;
import com.levigo.jadice.annotation.profiles.AnnotationProfile;
import com.levigo.jadice.swing.tool.MouseEditEvent;
import com.levigo.jadice.swing.tool.Tool;
public class StampAnnoTool extends Tool {
private boolean activated = false;
public final void setToolToActivated() {
// bei Aktivierung den Cursor setzen
setCursor((Cursor) UIManager.get("Jadice.cursors.cursor.crosshair"));
activated = true;
}
public final void setToolToDeactivated() {
// bei Deaktivierung den Standard-Cursor setzen
setCursor(Cursor.getDefaultCursor());
activated = false;
}
@Override
protected void handleMouseClicked(MouseEditEvent e, boolean isActive) {
if (!activated) {
super.handleMouseClicked(e, isActive);
return;
}
try {
// Hier den Dialog für die Stempelauswahl öffnen
System.err.println("---> Hier Dialog öffnen");
final boolean cancelAnnoCreation = false;
// Stempelannotation anlegen und konfigurieren, wenn im Dialog kein Abbruch des Anlegens vorgenommen wurde
if (!cancelAnnoCreation) {
final AnnotationFactory af = AnnotationProfile.getDefaultProfile().getTemplate("Stamp");
final StampAnnotation sa = (StampAnnotation) af.createNewInstance();
sa.setText("Erstellt via StampAnnoTool");
// hier dann weitere Einstellungen (Farbe, Liniendicke, usw.) aus dem Dialog setzen
...
// Position setzen
sa.setUseCenterAsOrigin(true);
sa.setLocation(e.getDocumentPoint().getX(), e.getDocumentPoint().getY());
sa.setUseCenterAsOrigin(false);
// AnnotationPageSegment holen
final AnnotationPageSegment aps = Annotations.getAnnotationPageSegment(e.getPage(), true);
// Erstellte Stempelanno der Seite hinzufügen
aps.addAnnotation(sa);
}
} catch (final Exception ex) {
ex.printStackTrace();
} finally {
// Tool deaktivieren
setToolToDeactivated();
// Exklusivmodus ausschalten
getManager().setNonExclusive(StampAnnoTool.class);
}
}
}
, multiple selections available, Use left or right arrow keys to navigate selected items