// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net) // All rights reserved. // // This file is part of Regionerate. // // 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. // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.ComponentModel; using System.Windows.Forms; using Rauchy.Regionerate.Presentation.Components.Properties; using Rauchy.Regionerate.ServiceLayer.Components; using Rauchy.Regionerate.ServiceLayer.Components.Services; using Rauchy.Regionerate.Shared.Components; namespace Rauchy.Regionerate.Presentation.Components { public static class RegionerateStarter { #region Fields (1)  private static CodeLayout _codeLayout; #endregion Fields  #region Properties (1)  /// /// Gets the Code Layout. /// /// The Code Layout. private static CodeLayout PrimaryCodeLayout { get { if ( _codeLayout == null ) { // First time the CodeLayout is loaded. LoadCodeLayout( Settings.Default ); // Sign up to be notified when settings change, so we could reload new CodeLayouts. Settings.Default.SettingsSaving += SettingsSaving; } return _codeLayout; } } #endregion Properties  #region Methods (8)  // Public Methods (3)  // [rgn] Public Methods (3) /// /// Regionerates the specified source code. /// /// The source code. /// The Regionerated version of unless /// user has cancelled the operation in the middle, in which case, will be returned. public static string HandleDocument( string sourceCode ) { CodeLayout codeLayout = GetPreferredLayout(); if ( codeLayout != null ) { DocumentService documentService = new DocumentService( codeLayout, Settings.Default.HolyWarPreventionMode ); Document document = new Document( "Text Block", sourceCode ); string regioneratedText = documentService.Process( document ); return regioneratedText; } else { // User has cancelled the operation. return sourceCode; } } /// /// Regionerates the specified project. /// /// The project path. /// Only C# projects are handled at the moment. public static void HandleProject( string projectPath ) { if ( MessageBox.Show( "This will affect every code file under this project. Do you want to continue?", "Regionerate", MessageBoxButtons.YesNo, MessageBoxIcon.Warning ) == DialogResult.Yes ) { ProjectService projectService = new CSharpProjectService( PrimaryCodeLayout, Settings.Default.HolyWarPreventionMode ); projectService.Process( projectPath ); } } /// /// Regionerates the specified solution. /// /// The solution path. public static void HandleSolution( string solutionPath ) { if ( MessageBox.Show( "This will affect every code file under this solution's projects. Do you want to continue?", "Regionerate", MessageBoxButtons.YesNo, MessageBoxIcon.Warning ) == DialogResult.Yes ) { SolutionService solutionService = new SolutionService( PrimaryCodeLayout, Settings.Default.HolyWarPreventionMode ); solutionService.Process( solutionPath ); } } // Private Methods (5)  private static Keys GetModifiers( string keyboardShortcut ) { Keys keys = Keys.None; if ( keyboardShortcut.Contains( "Ctrl" ) ) { keys |= Keys.Control; } if ( keyboardShortcut.Contains( "Alt" ) ) { keys |= Keys.Alt; } if ( keyboardShortcut.Contains( "Shift" ) ) { keys |= Keys.Shift; } return keys; } /// /// Determines which Code Layout to apply on documents. /// /// /// This is a new feature in v0.6.7.2 which allows the user to select from numerous layouts during Regioneration. /// If the user holds down the Ctrl key he will be presented with a selection form, /// similar to Windows' window selection form that appears when hitting Alt+Tab and holding Alt down. /// Edit: starting on v0.7.0.0, this feature is also available when clicking the Regionerate This menu item. /// /// The preferred if chosen; null if canceled. private static CodeLayout GetPreferredLayout() { string keyboardShortcut = Settings.Default.KeyboardShortcut; Keys modifiers = GetModifiers( keyboardShortcut ); bool modifierKeysDown = ( Control.ModifierKeys == modifiers ); if ( modifierKeysDown ) { // "Regionerate This" was called using the correct keyboard modifiers. // In this case, we show the Code Layout Browser in "key up" mode, meaning that // releasing the shortcutKey will choose a Code Layout. string shortcutKey = GetShortcutKey( keyboardShortcut ); CodeLayoutBrowser browser = new CodeLayoutBrowser( modifiers, shortcutKey, PrimaryCodeLayout ); if ( browser.ShowDialog() == DialogResult.OK ) { return browser.SelectedCodeLayout; } else { return null; } } else if ( ( Control.ModifierKeys == Keys.None ) && ( Settings.Default.AlwaysShowCodeLayoutBrowser ) ) { // "Regionerate This" was called with no keyboard modifiers, which means it was called // by clicking a menu button with the mouse. Also, the user has selected to always show the Code Layout Browser. // In this case, we show the Code Layout Browser in "double click" mode, meaning that // double clicking on an icon will choose a Code Layout. CodeLayoutBrowser browser = new CodeLayoutBrowser( PrimaryCodeLayout ); if ( browser.ShowDialog() == DialogResult.OK ) { return browser.SelectedCodeLayout; } else { return null; } } else { // The user has turned "Always Show Code Layout Browser" off, meaning that the only way he wants to // acccess the Code Layout Browser is by using the modifier keys, which he didn't, so we // just choose the primary Code Layout for him. return PrimaryCodeLayout; } } private static string GetShortcutKey( string keyboardShortcut ) { return keyboardShortcut[ keyboardShortcut.Length - 1 ].ToString(); } // [rgn] Private Methods (2) /// /// Loads the code layout. /// private static void LoadCodeLayout( Settings settings ) { if ( settings.UseDefaultCodeLayout ) { _codeLayout = CodeLayout.DefaultLayout; } else { try { _codeLayout = CodeLayout.Load( settings.CustomCodeLayoutPath ); } catch ( Exception ex ) { string message = string.Format( "There was an error loading the Code Layout document:\n\n{0}\n\nThe default Code Layout will be used.", ex ); MessageBox.Show( message, "Regionerate", MessageBoxButtons.OK, MessageBoxIcon.Error ); Settings.Default.UseDefaultCodeLayout = true; Settings.Default.Save(); } } } private static void SettingsSaving( object sender, CancelEventArgs e ) { // The settings have been modified, let's reload the CodeLayout just in case it was changed. LoadCodeLayout( Settings.Default ); } #endregion Methods  } }