srcanaapps/apiquerytool/com.nokia.s60tools.apiquery/src/com/nokia/s60tools/apiquery/ui/views/main/search/QueryDefComposite.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2  * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.s60tools.apiquery.ui.views.main.search;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.DataOutputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.FileNotFoundException;
       
    25 import java.io.FileOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStreamReader;
       
    28 import java.util.ArrayList;
       
    29 
       
    30 import org.eclipse.jface.fieldassist.AutoCompleteField;
       
    31 import org.eclipse.jface.fieldassist.TextContentAdapter;
       
    32 import org.eclipse.swt.SWT;
       
    33 import org.eclipse.swt.events.ModifyEvent;
       
    34 import org.eclipse.swt.events.ModifyListener;
       
    35 import org.eclipse.swt.events.SelectionEvent;
       
    36 import org.eclipse.swt.events.SelectionListener;
       
    37 import org.eclipse.swt.layout.GridData;
       
    38 import org.eclipse.swt.layout.GridLayout;
       
    39 import org.eclipse.swt.widgets.Button;
       
    40 import org.eclipse.swt.widgets.Composite;
       
    41 import org.eclipse.swt.widgets.Group;
       
    42 import org.eclipse.swt.widgets.Layout;
       
    43 import org.eclipse.swt.widgets.Text;
       
    44 import org.eclipse.ui.PlatformUI;
       
    45 
       
    46 import com.nokia.s60tools.apiquery.APIQueryHelpContextIDs;
       
    47 import com.nokia.s60tools.apiquery.shared.plugin.APIQueryPlugin;
       
    48 import com.nokia.s60tools.apiquery.shared.resources.Messages;
       
    49 import com.nokia.s60tools.apiquery.shared.searchmethod.ui.AbstractUiFractionComposite;
       
    50 import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
       
    51 import com.nokia.s60tools.util.debug.DbgUtility;
       
    52 
       
    53 /**
       
    54  * UI composite that shows the search field and Search button in Search tab.
       
    55  * 
       
    56  */
       
    57 class QueryDefComposite extends AbstractUiFractionComposite implements
       
    58 		SelectionListener, ModifyListener {
       
    59 
       
    60 	/**
       
    61 	 * Amount of columns in the used grid layout.
       
    62 	 */
       
    63 	private final int COLUMN_COUNT = 2;
       
    64 
       
    65 	private static String CACHE_NAME = "Cache.txt";
       
    66 
       
    67 	/**
       
    68 	 * Group giving common layout and containing controls-
       
    69 	 */
       
    70 	private Group searchStringDefinitionGroup;
       
    71 
       
    72 	/**
       
    73 	 * Search string entering field.
       
    74 	 */
       
    75 	private Text searchStringTxtField;
       
    76 
       
    77 	/**
       
    78 	 * Action button for starting the query.
       
    79 	 */
       
    80 	private Button runQueryBtn;
       
    81 
       
    82 	/**
       
    83 	 * 
       
    84 	 */
       
    85 	private IQueryDefCompositeListener queryActionListener = null;
       
    86 	static DataOutputStream dos;
       
    87 	public static String cacheFileName = APIQueryPlugin.getPluginWorkspacePath() + File.separator + CACHE_NAME;
       
    88 	public static ArrayList<String> wordsCaches = new ArrayList<String>();
       
    89 	public AutoCompleteField auto;
       
    90 	
       
    91 	
       
    92 	/**
       
    93 	 * Constructor.
       
    94 	 * 
       
    95 	 * @param parentComposite
       
    96 	 *            Parent composite for the created composite.
       
    97 	 */
       
    98 	public QueryDefComposite(Composite parentComposite) {
       
    99 		super(parentComposite);
       
   100 	}
       
   101 
       
   102 	/*
       
   103 	 * (non-Javadoc)
       
   104 	 * 
       
   105 	 * @see
       
   106 	 * com.nokia.s60tools.apiquery.ui.views.main.AbstractTabComposite#createLayout
       
   107 	 * ()
       
   108 	 */
       
   109 	protected Layout createLayout() {
       
   110 		return new GridLayout(COLUMN_COUNT, false);
       
   111 	}
       
   112 
       
   113 	/*
       
   114 	 * (non-Javadoc)
       
   115 	 * 
       
   116 	 * @seecom.nokia.s60tools.apiquery.ui.views.main.AbstractTabComposite#
       
   117 	 * createLayoutData()
       
   118 	 */
       
   119 	protected Object createLayoutData() {
       
   120 		return new GridData(GridData.FILL_HORIZONTAL);
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Set context sensitive help ids to components that can have focus
       
   125 	 * 
       
   126 	 */
       
   127 	private void setContextSensitiveHelpIds() {
       
   128 		try {
       
   129 			PlatformUI.getWorkbench().getHelpSystem().setHelp(
       
   130 					searchStringTxtField,
       
   131 					APIQueryHelpContextIDs.API_QUERY_HELP_SEARCH_TAB);
       
   132 			PlatformUI.getWorkbench().getHelpSystem().setHelp(runQueryBtn,
       
   133 					APIQueryHelpContextIDs.API_QUERY_HELP_SEARCH_TAB);
       
   134 		} catch (Exception e) {
       
   135 			e.printStackTrace();
       
   136 			APIQueryConsole
       
   137 					.getInstance()
       
   138 					.println(
       
   139 							Messages
       
   140 									.getString("QueryDefComposite.Context_ErrMsg") + e, APIQueryConsole.MSG_ERROR); //$NON-NLS-1$
       
   141 		}
       
   142 	}
       
   143 
       
   144 	/*
       
   145 	 * (non-Javadoc)
       
   146 	 * 
       
   147 	 * @see
       
   148 	 * com.nokia.s60tools.apiquery.ui.views.main.AbstractTabComposite#createControls
       
   149 	 * ()
       
   150 	 */
       
   151 	protected void createControls() {
       
   152 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION,
       
   153 				"-- createControls() --> " + getClass().getName()); //$NON-NLS-1$
       
   154 		searchStringDefinitionGroup = new Group(this, SWT.SHADOW_NONE);
       
   155 		searchStringDefinitionGroup.setText(Messages
       
   156 				.getString("QueryDefComposite.SearchString_Msg")); //$NON-NLS-1$
       
   157 
       
   158 		GridLayout gdl2 = new GridLayout(COLUMN_COUNT, false);
       
   159 		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
       
   160 
       
   161 		searchStringDefinitionGroup.setLayout(gdl2);
       
   162 		searchStringDefinitionGroup.setLayoutData(gd2);
       
   163 
       
   164 		final int textFieldStyleBits = SWT.LEFT | SWT.SINGLE | SWT.BACKGROUND
       
   165 				| SWT.BORDER;
       
   166 		searchStringTxtField = new Text(searchStringDefinitionGroup,
       
   167 				textFieldStyleBits);
       
   168 		searchStringTxtField.setLayoutData((new GridData(
       
   169 				GridData.FILL_HORIZONTAL)));
       
   170 		searchStringTxtField.setEditable(true);
       
   171 		searchStringTxtField.addModifyListener(this);
       
   172 		searchStringTxtField.addSelectionListener(this);
       
   173 
       
   174 		runQueryBtn = new Button(searchStringDefinitionGroup, SWT.PUSH);
       
   175 		runQueryBtn.setText(Messages.getString("QueryDefComposite.Search_Msg")); //$NON-NLS-1$
       
   176 		runQueryBtn.addSelectionListener(this);
       
   177 
       
   178 		// Query enabled by default
       
   179 		enableQuery();
       
   180 
       
   181 		// if it really exists
       
   182 		if (!isFileExists(cacheFileName)) {
       
   183 			writeToFile(cacheFileName, " \n", false);
       
   184 		} else {// load the entries into the array list
       
   185 			fileToHashTable(cacheFileName, wordsCaches);
       
   186 		}
       
   187 
       
   188 		auto = new AutoCompleteField(searchStringTxtField, new TextContentAdapter(), getKeys());
       
   189 
       
   190 		setContextSensitiveHelpIds();
       
   191 	}
       
   192 
       
   193 	/*
       
   194 	 * (non-Javadoc)
       
   195 	 * 
       
   196 	 * @see
       
   197 	 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
       
   198 	 * .swt.events.SelectionEvent)
       
   199 	 */
       
   200 	public void widgetDefaultSelected(SelectionEvent event) {
       
   201 		if (event.widget == searchStringTxtField) {
       
   202 			// <code>widgetDefaultSelected</code> is typically called
       
   203 			// when ENTER is pressed in a single-line text.
       
   204 			if (queryActionListener != null) {
       
   205 				queryActionListener.queryStarted(searchStringTxtField.getText(), false);
       
   206 				String value = searchStringTxtField.getText();
       
   207 				putKey(value);
       
   208 				auto.setProposals(getKeys());
       
   209 			}
       
   210 		}
       
   211 	}
       
   212 
       
   213 	/*
       
   214 	 * (non-Javadoc)
       
   215 	 * 
       
   216 	 * @see
       
   217 	 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
       
   218 	 * .events.SelectionEvent)
       
   219 	 */
       
   220 	public void widgetSelected(SelectionEvent event) {
       
   221 		if (event.widget == runQueryBtn) {
       
   222 			if (queryActionListener != null) {
       
   223 				queryActionListener.queryStarted(searchStringTxtField.getText(), false);
       
   224 				String value = searchStringTxtField.getText();
       
   225 				putKey(value);
       
   226 				auto.setProposals(getKeys());
       
   227 			}
       
   228 			/*else if (event.widget == searchStringTxtField) {
       
   229 				// <code>widgetDefaultSelected</code> is typically called
       
   230 				// when ENTER is pressed in a single-line text.
       
   231 				if (queryActionListener != null) {
       
   232 					String value = searchStringTxtField.getText();
       
   233 					queryActionListener.queryStarted(
       
   234 							searchStringTxtField.getText(), false);
       
   235 					putKey(wordsCaches, value, cacheFileName);
       
   236 					//auto.setProposals(getKeys(wordsCaches));
       
   237 				}
       
   238 			}*/
       
   239 
       
   240 		}
       
   241 	}
       
   242 
       
   243 	/**
       
   244 	 * Adds listener that listens for query actions.
       
   245 	 * 
       
   246 	 * @param queryActionListener
       
   247 	 *            Query action listener.
       
   248 	 */
       
   249 	public void setCompositeListener(
       
   250 			IQueryDefCompositeListener queryActionListener) {
       
   251 		this.queryActionListener = queryActionListener;
       
   252 	}
       
   253 
       
   254 	/**
       
   255 	 * Disables query button.
       
   256 	 */
       
   257 	public void disableQuery() {
       
   258 		runQueryBtn.setEnabled(false);
       
   259 	}
       
   260 
       
   261 	/**
       
   262 	 * Enables query button.
       
   263 	 */
       
   264 	public void enableQuery() {
       
   265 		runQueryBtn.setEnabled(true);
       
   266 	}
       
   267 
       
   268 	/*
       
   269 	 * (non-Javadoc)
       
   270 	 * 
       
   271 	 * @see
       
   272 	 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
       
   273 	 * .ModifyEvent)
       
   274 	 */
       
   275 	public void modifyText(ModifyEvent arg0) {
       
   276 		if (queryActionListener != null) {
       
   277 			queryActionListener.queryModified(searchStringTxtField.getText());
       
   278 		}
       
   279 	}
       
   280 
       
   281 	/*
       
   282 	 * (non-Javadoc)
       
   283 	 * 
       
   284 	 * @see org.eclipse.swt.widgets.Widget#dispose()
       
   285 	 */
       
   286 	public void dispose() {
       
   287 		super.dispose();
       
   288 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION,"-- Dispose() --> " + getClass().getName());
       
   289 		searchStringDefinitionGroup.dispose();
       
   290 		searchStringTxtField.dispose();
       
   291 		runQueryBtn.dispose();
       
   292 	}
       
   293 
       
   294 	/**
       
   295 	 * Sets the query string.
       
   296 	 * 
       
   297 	 * @param queryString
       
   298 	 *            Query string to be set.
       
   299 	 */
       
   300 	public void setQueryString(String queryString) {
       
   301 		searchStringTxtField.setText(queryString);
       
   302 	}
       
   303 
       
   304 	public boolean writeToFile(String fileName, String dataLine, boolean isAppendMode) {
       
   305 
       
   306 		try {
       
   307 			File outFile = new File(fileName);
       
   308 			if (isAppendMode) {
       
   309 				dos = new DataOutputStream(new FileOutputStream(fileName, true));
       
   310 			} else {
       
   311 				dos = new DataOutputStream(new FileOutputStream(outFile));
       
   312 			}
       
   313 
       
   314 			dos.writeBytes(dataLine);
       
   315 			dos.close();
       
   316 		} catch (FileNotFoundException ex) {
       
   317 			return (false);
       
   318 		} catch (IOException ex) {
       
   319 			return (false);
       
   320 		}
       
   321 		return (true);
       
   322 
       
   323 	}
       
   324 
       
   325 	public boolean isFileExists(String fileName) {
       
   326 		File file = new File(fileName);
       
   327 		return file.exists();
       
   328 	}
       
   329 
       
   330 	public boolean deleteFile(String fileName) {
       
   331 		File file = new File(fileName);
       
   332 		return file.delete();
       
   333 	}
       
   334 
       
   335 	public void fileToHashTable(String fileName, ArrayList<String> arr) {
       
   336 		String inputLine;
       
   337 		try {
       
   338 			File inFile = new File(fileName);
       
   339 			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
       
   340 			while ((inputLine = br.readLine()) != null) {
       
   341 				if(wordsCaches.indexOf(inputLine.trim())==-1)
       
   342 					wordsCaches.add(inputLine.trim());
       
   343 			}
       
   344 			br.close();
       
   345 		}
       
   346 		catch (FileNotFoundException ex) {
       
   347 			ex.printStackTrace();
       
   348 		} catch (IOException ex) {
       
   349 			ex.printStackTrace();
       
   350 		}
       
   351 	}
       
   352 
       
   353 	// puts key to both file and hash table
       
   354 	public void putKey(String value) {
       
   355 		if (wordsCaches.contains(value))
       
   356 			return;
       
   357 		wordsCaches.add(value.trim());
       
   358 		writeToFile(cacheFileName, value.trim() + "\n", true);
       
   359 	}
       
   360 
       
   361 	// readhashTableKeys
       
   362 	public String[] getKeys() {
       
   363 		return wordsCaches.toArray(new String[wordsCaches.size()]);
       
   364 	}
       
   365 }