CIB jView form technischer Leitfaden

6. Schneller Einstieg: Einbindung von JView&form&sign in eine Java Swing-Applikation

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.cib.comod.jobs.ICibFormatJob;

import de.cib.gui.framework.ICibApplication;
import de.cib.gui.framework.ICibRecApplication;
import de.cib.gui.framework.JCibApplication;
import de.cib.gui.util.trace.JCibTrace;
import de.cib.view.modules.form.FormFieldCallback;
import de.cib.view.modules.form.FormFieldInfo;


public class JCibViewFormSignRunner {

	public static final String TEST_FILENAME = "test_form.pdf";

	public static void main(java.lang.String[] args) {
		// activate / deactivate traces
		JCibView.enableDebug(true);
		JCibTrace.setTraceLevel(JCibTrace.LEVEL_INFO);
		 
		// create a new viewer instance
		JCibView viewer = new JCibView();
		
		// set license information to the viewer
		setLicenseInformation(viewer);
		
		// set input file (optional)
		viewer.setProperty(ICibApplication.PROPERTY_INPUTFILE, TEST_FILENAME);
		
		// create a new jrec2 instance and set properties
		initJrec2(viewer);
		
		// set programmatically properties to the viewer
		setViewerProperties(viewer);
		
		// set properties for &form
		setFormProperties(viewer);
		
		// set properties for &sign
		setSignProperties(viewer);
		
		// set properties to the viewer given by command line
		setCommandLineProperties(viewer, args);
		
		// create and show GUI
		createAndShowGUI(viewer);
		
		// register FormFieldCallbackTester
		viewer.setFormFieldCallback(new FormFieldCallbackTester());

	}

	private static void createAndShowGUI(final JCibView viewer) {

		// create a test jframe to embedd the viewer in it.
		final JFrame viewerFrame = createViewerFrame();

		// connect the test frame to the viewer
		viewer.setFrame(viewerFrame);
		viewerFrame.getContentPane().add(viewer, BorderLayout.CENTER);

		// couple window closing to exit hook of viewer application
		viewerFrame.setDefaultCloseOperation(
			WindowConstants.DO_NOTHING_ON_CLOSE);
		viewerFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Action t_action = viewer.getActionMap()
					.get(ICibApplication.ACTION_EXIT);
				if (t_action != null) 
					t_action.actionPerformed(null);

				System.exit(0);
			}
		});

		// here special exit operations can be implemented 
		// which will be executed when the viewer gets closed
		viewer.setExitHook(new Runnable() {
			public void run() {
				// do post processing here
				viewer.stop();

				// unregister FormFieldCallbackTester
				viewer.setFormFieldCallback(null);
			}
		});

		// start and run the viewer
		viewer.load();
		viewer.start();

		// show frame in which the viewer is embedded
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				viewerFrame.setVisible(true);
			}
		});
	}

	private static void initJrec2(JCibView viewer) {
		// create jrec2 instance without starting it
		JCibApplication t_cibRec = viewer.getRec(false);
		if (t_cibRec != null) {
			// embedded jrec2 doesn't need Jrec-Ini file
			t_cibRec.setProperty(ICibRecApplication.
				PROPERTY_JREC_INIFILENAME, null);

			// Activate OK/Cancel icons in JRec toolbar
			t_cibRec.setProperty(ICibRecApplication.
				PROPERTY_REC_SHOW_OK_CANCEL_ICON, Boolean.TRUE);

			// Enable hyphenation
			t_cibRec.setProperty(ICibRecApplication.
				PROPERTY_HYPHENATION_ENABLED, Boolean.TRUE);
			
			// Set hyphenation dictionary path
			t_cibRec.setProperty(ICibRecApplication.
				PROPERTY_HYPHENATION_DICTIONARY_PATH, "./lib/dict/");
			
			// Enable spell checking
			t_cibRec.setProperty(ICibRecApplication.
				PROPERTY_SPELLCHECKING_ENABLED, Boolean.TRUE);
			
			// Set spell checking dictionary path
			t_cibRec.setProperty(ICibRecApplication.
					PROPERTY_SPELLCHECKING_DICTIONARY_PATH, 
					"./lib/dict/");

			try {
				String currentDir = new java.io.File(".")
					.getCanonicalPath();
				currentDir += "/lib";
				JCibTrace.info("Current lib directory: " 
					+ currentDir);
				currentDir += "/win32";
				t_cibRec.setProperty(ICibRecApplication.
					PROPERTY_SPELLCHECKING_NATIVE_LIBRARY_PATH, 
					currentDir);
				t_cibRec.setProperty(ICibRecApplication.
					PROPERTY_SPELLCHECKING_LANGUAGE, "de_DE");
			} catch (IOException e) {
				e.getMessage();
			}
		}
	}

	private static JFrame createViewerFrame() {
		JFrame res = new JFrame();
		res.setTitle("CIB JView&form&sign");
		
		// 75 % of the screen width and height 
		int frameWidth = (int) ((Toolkit.getDefaultToolkit()
			.getScreenSize().getWidth()) * 0.75);
		int frameHeight = (int) ((Toolkit.getDefaultToolkit()
			.getScreenSize().getHeight()) * 0.75);
		
		// restore the frame's last position
		res.setSize(frameWidth, frameHeight);
		return res;
	}
	
	private static void setLicenseInformation(JCibView viewer) {
		viewer.setProperty(ICibFormatJob.PROPERTY_LICENSECOMPANY, "");
		viewer.setProperty(ICibFormatJob.PROPERTY_LICENSEKEY, "");
	}
	
	private static void setViewerProperties(JCibView viewer) {
		viewer.setProperty(
			ICibApplication.PROPERTY_MENU_ENABLED, Boolean.FALSE);

		viewer.setProperty(
			ICibApplication.PROPERTY_DRAG_AND_DROP_ENABLED, 
			Boolean.TRUE);

		viewer.setProperty(
			ICibApplication.PROPERTY_SHOWJPRINTDIALOG, Boolean.FALSE);

		viewer.setProperty(
			ICibApplication.PROPERTY_THUMBNAILS_ENABLED, Boolean.TRUE);
	}
	
	private static void setFormProperties(JCibView viewer){
		
		viewer.setProperty(
			ICibApplication.PROPERTY_FORM_ENABLED, Boolean.TRUE);
		
		/*
		 * Type: Integer: 1=red, 2=yellow, 3=magenta, 4=cyan, 
		 * 5=gray, 6=green, 7=blue, 8=back.
		 * Default: 7
		 */
		viewer.setProperty(ICibApplication.
			PROPERTY_DEFAULT_FORM_HIGHLIGHTING_COLOR, 6);

		/*
		 * Type: Integer: 0-255
		 * Default: 100
		 */
		viewer.setProperty(ICibApplication.
			PROPERTY_DEFAULT_FORM_HIGHLIGHTING_COLOR_TRANSPARENCY, 75);
	
		/* 
		 * Type: Boolean
		 * Default: true
	 	 */
//		viewer.setProperty(ICibApplication.
//			PROPERTY_FORM_SIGNED_LOCK_ENABLED, Boolean.FALSE);
		
	}
	
	private static void setSignProperties(JCibView viewer){
		/* 
		 * Type: Boolean
		 * Default: false
	 	 */
		viewer.setProperty(ICibApplication.
			PROPERTY_SIGN_ENABLED, Boolean.TRUE);

		/* 
		 * Type: Boolean
		 * Default: true
		 */
		viewer.setProperty(ICibApplication.
			PROPERTY_SIGN_FREE_SIGN_ENABLED, Boolean.FALSE);

		/* 
		 * Type: String
		 * Default: ""
	 	 */
		viewer.setProperty(ICibApplication.
			PROPERTY_SIGN_PAD_PROVIDER, "signotec-signpad");
		/* 
		 * Type: Boolean
		 * Default: true
		 */
//		viewer.setProperty(ICibApplication.
//			PROPERTY_SIGN_NOT_FILLED_FORM_WARNING, Boolean.FALSE);

		/* 
		 * Type: Integer
		 * Default: 3000 Bytes
		 */
		viewer.setProperty(ICibApplication.
			PROPERTY_SIGN_REQUIRED_SIZE, 1000);
		
		// Generate DigSig fields (RTF case)
		viewer.setProperty("UseDigitalSignatureWidgets", 1);
		viewer.setProperty("NeedAppearancesSignatureWidgets", 0);
		
	}
	
	private static void setCommandLineProperties(

		JCibView viewer, String[] args) {
		// set properties which were set via command line
		if (args.length == 0) 
			return;
		
		for (int i = 0; i < args.length; i++) {
			String argument = args[i];
			if (argument == null)
				continue;

			// ignore tracing, it is set to true by default
			if (argument.equals("-t"))
				continue;

			// a property is set as "-propertyname value" 
			// or as "inputfile" 
			if (argument.startsWith("-")) {
				viewer.setProperty(argument.substring(1), args[i+1]);
				i++;
			} else {
				viewer.setProperty(ICibApplication.
					PROPERTY_INPUTFILE, args[i]);
			}
		}
	}
}