Java tutorial
/* * Copyright (C) 2012 NS Solutions Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.htmlhifive.tools.codeassist.ui.config; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.ui.PlatformUI; import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLogger; import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLoggerFactory; import com.htmlhifive.tools.codeassist.core.messages.Messages; import com.htmlhifive.tools.codeassist.ui.H5CodeAssistUIPlugin; import com.htmlhifive.tools.codeassist.ui.messages.UIMessages; import com.htmlhifive.tools.codeassist.ui.view.bean.OptionConfigureBean; /** * ???. * * @author NS Solutions Corporation * */ public class CodeAssistConfig { /** * ?. */ private static final String CONFIG_KEY_PREFIX = "hi5.tool.codeassist."; /** * . */ private static final String KEY_OPTION_PATH = CONFIG_KEY_PREFIX + "option"; /** * . */ private static H5CodeAssistPluginLogger logger = H5CodeAssistPluginLoggerFactory .getLogger(CodeAssistConfig.class); /** * . */ private IFile configProp; /** * . */ private final OptionConfigureBean configBean; /** * . * * @param file . */ CodeAssistConfig(IFile file) { configBean = new OptionConfigureBean(); configProp = file; load(); } /** * ?. */ void load() { try { configProp.refreshLocal(IResource.DEPTH_ZERO, null); if (configProp.exists()) { Properties properties = new Properties(); properties.load(configProp.getContents()); configBean.setOptionFilePath(properties.getProperty(KEY_OPTION_PATH, "")); } } catch (CoreException e) { logger.log(Messages.EM0001, e); } catch (IOException e) { logger.log(Messages.EM0001, e); } } /** * ??. * * @return . */ public OptionConfigureBean getConfigBean() { return configBean; } /** * ???. * * @return ???????? */ public boolean saveConfig() { Properties properties = new Properties(); properties.setProperty(KEY_OPTION_PATH, configBean.getOptionFilePath()); ByteArrayOutputStream output = new ByteArrayOutputStream(); InputStream input = null; try { properties.store(output, ""); output.flush(); input = new ByteArrayInputStream(output.toByteArray()); configProp.refreshLocal(IResource.DEPTH_ZERO, null); if (configProp.exists()) { configProp.setContents(input, false, false, null); } else { configProp.create(input, false, null); } return true; } catch (CoreException e) { logger.log(UIMessages.UIEM0002, e); ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), UIMessages.UIDT0002.getText(), UIMessages.UIEM0002.getText(), e.getStatus()); } catch (IOException e) { logger.log(UIMessages.UIEM0002, e); ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), UIMessages.UIDT0002.getText(), UIMessages.UIEM0002.getText(), new Status(IStatus.ERROR, H5CodeAssistUIPlugin.PLUGIN_ID, e.getMessage())); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } return false; } }