All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xy.reflect.ui.control.swing.customizer.SwingCustomizer Maven / Gradle / Ivy

There is a newer version: 5.2.10
Show newest version
/*******************************************************************************
 * Copyright (C) 2018 OTK Software
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * The GNU General Public License allows you also to freely redistribute 
 * the libraries under the same license, if you provide the terms of the 
 * GNU General Public License with them and add the following 
 * copyright notice at the appropriate place (with a link to 
 * http://javacollection.net/reflectionui/ web site when possible).
 ******************************************************************************/
package xy.reflect.ui.control.swing.customizer;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import xy.reflect.ui.CustomizedUI;
import xy.reflect.ui.control.swing.renderer.CustomizedSwingRenderer;
import xy.reflect.ui.control.swing.renderer.FieldControlPlaceHolder;
import xy.reflect.ui.control.swing.renderer.Form;
import xy.reflect.ui.control.swing.renderer.MethodControlPlaceHolder;
import xy.reflect.ui.control.swing.renderer.SwingRenderer;
import xy.reflect.ui.info.InfoCategory;
import xy.reflect.ui.info.custom.InfoCustomizations;
import xy.reflect.ui.info.field.IFieldInfo;
import xy.reflect.ui.info.filter.IInfoFilter;
import xy.reflect.ui.info.method.IMethodInfo;
import xy.reflect.ui.info.type.ITypeInfo;
import xy.reflect.ui.info.type.source.JavaTypeInfoSource;
import xy.reflect.ui.util.MoreSystemProperties;
import xy.reflect.ui.util.ReflectionUIError;
import xy.reflect.ui.util.ReflectionUIUtils;
import xy.reflect.ui.util.SwingCustomizerUtils;
import xy.reflect.ui.util.SwingRendererUtils;
import xy.reflect.ui.util.SystemProperties;
import xy.reflect.ui.util.component.ControlPanel;

public class SwingCustomizer extends CustomizedSwingRenderer {

	public static void main(String[] args) throws Exception {
		Class clazz = Object.class;
		String usageText = "Expected arguments: [  | --help ]"
				+ "\n  => : Fully qualified name of a class to instanciate and display in a window"
				+ "\n  => --help: Displays this help message" + "\n"
				+ "\nAdditionally, the following JVM properties can be set:" + "\n" + MoreSystemProperties.describe();
		if (args.length == 0) {
			clazz = Object.class;
		} else if (args.length == 1) {
			if (args[0].equals("--help")) {
				System.out.println(usageText);
				return;
			} else {
				clazz = Class.forName(args[0]);
			}
		} else {
			throw new IllegalArgumentException(usageText);
		}
		Object object = SwingCustomizer.getDefault().onTypeInstanciationRequest(null,
				SwingCustomizer.getDefault().getReflectionUI().getTypeInfo(new JavaTypeInfoSource(clazz, null)), null);
		if (object == null) {
			return;
		}
		SwingCustomizer.getDefault().openObjectFrame(object);
	}

	public static final String CUSTOMIZATIONS_FORBIDDEN_PROPERTY_KEY = SwingRenderer.class.getName()
			+ ".CUSTOMIZATIONS_FORBIDDEN";

	protected static SwingCustomizer defaultInstance;

	protected CustomizationTools customizationTools;
	protected CustomizationOptions customizationOptions;
	protected CustomizationController customizationController;
	protected String infoCustomizationsOutputFilePath;

	public static SwingCustomizer getDefault() {
		if (defaultInstance == null) {
			defaultInstance = new SwingCustomizer(CustomizedUI.getDefault());
		}
		return defaultInstance;
	}

	public SwingCustomizer(CustomizedUI customizedUI, String infoCustomizationsOutputFilePath) {
		super(customizedUI);
		if (infoCustomizationsOutputFilePath != null) {
			File file = new File(infoCustomizationsOutputFilePath);
			if (file.exists()) {
				try {
					getInfoCustomizations().loadFromFile(file,
							ReflectionUIUtils.getDebugLogListener(getCustomizedUI()));
				} catch (IOException e) {
					throw new ReflectionUIError(e);
				}
			} else {
				try {
					getInfoCustomizations().saveToFile(file, ReflectionUIUtils.getDebugLogListener(getCustomizedUI()));
				} catch (IOException e) {
					throw new ReflectionUIError(e);
				}
			}
		}
		this.infoCustomizationsOutputFilePath = infoCustomizationsOutputFilePath;
		this.customizationTools = createCustomizationTools();
		this.customizationOptions = createCustomizationOptions();
		this.customizationController = createCustomizationController();
	}

	public SwingCustomizer(CustomizedUI customizedUI) {
		this(customizedUI, SystemProperties.getDefaultInfoCustomizationsFilePath());
	}

	public String getInfoCustomizationsOutputFilePath() {
		return infoCustomizationsOutputFilePath;
	}

	public CustomizationController createCustomizationController() {
		return new CustomizationController(this);
	}

	public CustomizationOptions createCustomizationOptions() {
		return new CustomizationOptions(this);
	}

	public CustomizationTools createCustomizationTools() {
		return new CustomizationTools(this);
	}

	public CustomizationTools getCustomizationTools() {
		return customizationTools;
	}

	public CustomizationOptions getCustomizationOptions() {
		return customizationOptions;
	}

	public CustomizationController getCustomizationController() {
		return customizationController;
	}

	@Override
	public CustomizingForm createForm(Object object, IInfoFilter infoFilter) {
		return new CustomizingForm(this, object, infoFilter);
	}

	public boolean isCustomizationsEditorEnabled() {
		return (infoCustomizationsOutputFilePath != null) && !MoreSystemProperties.areCustomizationToolsDisabled();
	}

	public ImageIcon getCustomizationsIcon() {
		return SwingCustomizerUtils.CUSTOMIZATION_ICON;
	}

	public boolean areCustomizationsEditable(Object object) {
		ITypeInfo type = reflectionUI.getTypeInfo(reflectionUI.getTypeInfoSource(object));
		if (!isCustomizationsEditorEnabled()) {
			return false;
		}
		if (!customizationOptions.isInEditMode()) {
			return false;
		}
		if (!getInfoCustomizations()
				.equals(type.getSpecificProperties().get(InfoCustomizations.CURRENT_CUSTOMIZATIONS_KEY))) {
			return false;
		}
		if (Boolean.TRUE.equals(type.getSpecificProperties().get(CUSTOMIZATIONS_FORBIDDEN_PROPERTY_KEY))) {
			return false;
		}
		return true;
	}

	public class CustomizingForm extends Form {
		private static final long serialVersionUID = 1L;

		protected boolean toolsAdded;

		public CustomizingForm(SwingRenderer swingRenderer, Object object, IInfoFilter infoFilter) {
			super(swingRenderer, object, infoFilter);
			if (isCustomizationsEditorEnabled()) {
				addAncestorListener(new AncestorListener() {

					@Override
					public void ancestorRemoved(AncestorEvent event) {
						getCustomizationController().formRemoved(CustomizingForm.this);
					}

					@Override
					public void ancestorMoved(AncestorEvent event) {
					}

					@Override
					public void ancestorAdded(AncestorEvent event) {
						getCustomizationController().formAdded(CustomizingForm.this);
					}
				});
			}
		}

		public boolean isToolsAdded() {
			return toolsAdded;
		}

		@Override
		public void layoutMemberControls(
				Map> fieldControlPlaceHoldersByCategory,
				Map> methodControlPlaceHoldersByCategory,
				JPanel membersPanel) {
			if (areCustomizationsEditable(object)) {
				membersPanel.setLayout(new BorderLayout());
				JPanel newMembersPanel = new ControlPanel();
				{
					membersPanel.add(newMembersPanel, BorderLayout.CENTER);
					Border newMembersPanelBorder;
					{
						int borderThickness = 2;
						newMembersPanelBorder = BorderFactory.createLineBorder(
								getCustomizationTools().getToolsRenderer().getToolsForegroundColor(), borderThickness);
						newMembersPanelBorder = BorderFactory.createCompoundBorder(newMembersPanelBorder,
								BorderFactory.createLineBorder(
										getCustomizationTools().getToolsRenderer().getToolsBackgroundColor(),
										borderThickness));
						newMembersPanelBorder = BorderFactory.createCompoundBorder(newMembersPanelBorder,
								BorderFactory.createLineBorder(
										getCustomizationTools().getToolsRenderer().getToolsForegroundColor(),
										borderThickness));
						newMembersPanel.setBorder(newMembersPanelBorder);
					}
					super.layoutMemberControls(fieldControlPlaceHoldersByCategory, methodControlPlaceHoldersByCategory,
							newMembersPanel);
				}
				JPanel typeCustomizationsControl = new ControlPanel();
				{
					typeCustomizationsControl.setLayout(new BorderLayout());
					typeCustomizationsControl.add(customizationTools.makeButtonForTypeInfo(object),
							BorderLayout.CENTER);
					membersPanel.add(
							SwingRendererUtils.flowInLayout(typeCustomizationsControl, GridBagConstraints.CENTER),
							BorderLayout.NORTH);
				}
				toolsAdded = true;
			} else {
				super.layoutMemberControls(fieldControlPlaceHoldersByCategory, methodControlPlaceHoldersByCategory,
						membersPanel);
				toolsAdded = false;
			}
		}

		@Override
		public void refresh(boolean refreshStructure) {
			if (areCustomizationsEditable(object) != toolsAdded) {
				removeAll();
				fieldControlPlaceHoldersByCategory.clear();
				methodControlPlaceHoldersByCategory.clear();
				super.refresh(true);
			} else {
				super.refresh(refreshStructure);
			}
		}

		@Override
		public CustomizingFieldControlPlaceHolder createFieldControlPlaceHolder(IFieldInfo field) {
			return new CustomizingFieldControlPlaceHolder((SwingCustomizer) swingRenderer, this, field);
		}

		@Override
		public CustomizingMethodControlPlaceHolder createMethodControlPlaceHolder(IMethodInfo method) {
			return new CustomizingMethodControlPlaceHolder((SwingCustomizer) swingRenderer, this, method);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy