« prev next »
Java Docking Home> Developer Guide> How to Use Dock Model Encoders and Decoders

How to Use Dock Model Encoders and Decoders

:: Saving your Workspace with Dock Model Encoders and Decoders ::

When the application is stopped, you can save the state of your DockModel. When the application is restarted later, you can put the docks, dockables, visualizers, and docking paths in the same state as they were saved.

:: Dock Model Encoder ::

You can use a DockModelEncoder to save your dock model:

		// Save the dock model.
		DockModelPropertiesEncoder encoder = new DockModelPropertiesEncoder();
		if (encoder.canSave(dockModel))
		{
			try
			{
				encoder.save(dockModel);
			}
			catch (Exception e)
			{
				System.out.println("Error while saving the dock model.");
				e.printStackTrace();
			}
		}
If you want to use this save methods, the dock model should have a source file path that is not null.
				String source = dockModel.getSource();
Otherwise you have to use the export methods. You can give a destination path to these methods:
				encoder.export(dockModel, destination);

:: Dock Model Decoder ::

You can use a DockModelDecoder to reload your dock model. You have to give the folowing things to the decoder:

		// Try to decode the dock model from file.
		DockModelPropertiesDecoder dockModelDecoder = new DockModelPropertiesDecoder();
		if (dockModelDecoder.canDecodeSource(SOURCE))
		{
			try 
			{
				// Create the map with the dockables, that the decoder needs.
				Map dockablesMap = new HashMap();
				for (int index = 0; index < dockables.length; index++)
				{
					dockablesMap.put( dockables[index].getID(), dockables[index]);
				}	
				for (int index = 0; index < buttonDockables.length; index++)
				{
					dockablesMap.put( buttonDockables[index].getID(), buttonDockables[index]);
				}			
								
				// Create the map with the owner windows, that the decoder needs.
				Map ownersMap = new HashMap();
				ownersMap.put(frameId, frame);
				
				// Create the map with the visualizers, that the decoder needs.
				Map visualizersMap = new HashMap();
				visualizersMap.put("maximizer", maximizer);
				visualizersMap.put("minimizer", minimizer);

				// Decode the file.
				dockModel = (FloatDockModel)dockModelDecoder.decode(SOURCE, dockablesMap, ownersMap, visualizersMap);
			}
			catch (FileNotFoundException fileNotFoundException){
				System.out.println("Could not find the file [" + SOURCE + "] with the saved dock model.");
				System.out.println("Continuing with the default dock model.");
			}
			catch (IOException ioException){
				System.out.println("Could not decode a dock model: [" + ioException + "].");
				ioException.printStackTrace();
				System.out.println("Continuing with the default dock model.");
			}
		}



The source code of the samples can be found in:

CodecExampleShows the usage, the encoding, and the decoding of dock models.
WorkspaceExampleShows the encoding and decoding of dock models and docking paths.



« prev next »