PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ProcedureEditor/frmProcedureEditor.pas

http://dbaexplorer.googlecode.com/
Pascal | 474 lines | 394 code | 55 blank | 25 comment | 40 complexity | 631cef075dec96e3a772151fba16bb42 MD5 | raw file
  1. {
  2. DBAExplorer - Oracle Admin Management Tool
  3. Copyright (C) 2008 Alpaslan KILICKAYA
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. }
  15. unit frmProcedureEditor;
  16. interface
  17. uses
  18. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  19. Dialogs, ComCtrls, cxGraphics, dxStatusBar, cxSplitter, cxListView,
  20. cxControls, cxContainer, cxEdit, cxTextEdit, cxMemo, cxRichEdit, dxBar,
  21. ExtCtrls, OraProcSource, OraBarConn, frmProcedureDetail, StdCtrls,RichEdit,
  22. ora, oraStorage, cxPC;
  23. type
  24. TProcedureEditorFrm = class(TForm)
  25. bottomPanel: TPanel;
  26. lviewError: TcxListView;
  27. cxSplitter2: TcxSplitter;
  28. dxBarManager1: TdxBarManager;
  29. btnSaveQuery: TdxBarButton;
  30. btnCompile: TdxBarButton;
  31. btnNewQuery: TdxBarButton;
  32. btnOpenQuery: TdxBarButton;
  33. btnUndo: TdxBarButton;
  34. btnRedo: TdxBarButton;
  35. btnFindText: TdxBarButton;
  36. btnAlignLeft: TdxBarButton;
  37. btnAlignCenter: TdxBarButton;
  38. btnAlignRight: TdxBarButton;
  39. btnCut: TdxBarButton;
  40. btnCopy: TdxBarButton;
  41. btnPaste: TdxBarButton;
  42. btnClear: TdxBarButton;
  43. btnSelectAll: TdxBarButton;
  44. btnReplace: TdxBarButton;
  45. dxBarDockControl1: TdxBarDockControl;
  46. OpenDialog: TOpenDialog;
  47. editorStatusBar: TdxStatusBar;
  48. SaveDialog: TSaveDialog;
  49. ReplaceDialog: TReplaceDialog;
  50. FindDialog: TFindDialog;
  51. bbtnRun: TdxBarButton;
  52. pc: TcxPageControl;
  53. tsSource: TcxTabSheet;
  54. tsBody: TcxTabSheet;
  55. SQLEditorSource: TRichEdit;
  56. SQLEditorBody: TRichEdit;
  57. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  58. procedure btnNewQueryClick(Sender: TObject);
  59. procedure btnOpenQueryClick(Sender: TObject);
  60. procedure btnSaveQueryClick(Sender: TObject);
  61. procedure SQLEditorSourceChange(Sender: TObject);
  62. procedure SQLEditorSourceSelectionChange(Sender: TObject);
  63. procedure btnUndoClick(Sender: TObject);
  64. procedure btnRedoClick(Sender: TObject);
  65. procedure btnCutClick(Sender: TObject);
  66. procedure btnCopyClick(Sender: TObject);
  67. procedure btnPasteClick(Sender: TObject);
  68. procedure btnClearClick(Sender: TObject);
  69. procedure btnSelectAllClick(Sender: TObject);
  70. procedure btnFindTextClick(Sender: TObject);
  71. procedure btnReplaceClick(Sender: TObject);
  72. procedure btnAlignLeftClick(Sender: TObject);
  73. procedure FindDialogFind(Sender: TObject);
  74. procedure ReplaceDialogFind(Sender: TObject);
  75. procedure btnCompileClick(Sender: TObject);
  76. procedure bbtnRunClick(Sender: TObject);
  77. procedure pcChange(Sender: TObject);
  78. private
  79. { Private declarations }
  80. FProcSource: TProcSource;
  81. procedure NewProcSource;
  82. procedure SetErrorStatus;
  83. function GetSQLEditor: TRichEdit;
  84. function GetSQLEditorCol: Integer;
  85. function GetSQLEditorRow: Integer;
  86. property SQLEditorCol: Integer read GetSQLEditorCol;
  87. property SQLEditorRow: Integer read GetSQLEditorRow;
  88. property SQLEditor: TRichEdit read GetSQLEditor;
  89. public
  90. { Public declarations }
  91. procedure Init(ProcSource : TProcSource);
  92. end;
  93. var
  94. ProcedureEditorFrm: TProcedureEditorFrm;
  95. implementation
  96. uses frmMain, util, frmProcedureRun, VisualOptions, GenelDM;
  97. {$R *.dfm}
  98. procedure TProcedureEditorFrm.Init(ProcSource : TProcSource);
  99. begin
  100. DMGenel.ChangeLanguage(self);
  101. ChangeVisualGUI(self);
  102. Caption := ProcSource.OraSession.Server+'/'+ProcSource.OraSession.UserName+' - '+'Procedure Editor' +' ('+ProcSource.SOURCE_NAME+')';
  103. editorStatusBar.Panels[0].Text := ProcSource.OraSession.Server+'/'+ProcSource.OraSession.UserName;
  104. MainFrm.dxBarListWindows.Items.AddObject(Caption, Self);
  105. FProcSource := TProcSource.Create;
  106. FProcSource := ProcSource;
  107. tsBody.TabVisible := FProcSource.SOURCE_TYPE = stPackage;
  108. if FProcSource.Mode = InsertMode then
  109. begin
  110. SQLEditorSource.Text := FProcSource.GetDefaultDDL;
  111. if FProcSource.SOURCE_TYPE = stPackage then
  112. SQLEditorBody.Text := FProcSource.GetDefaultBodyDDL;
  113. SQLEditor.Modified := True;
  114. end else
  115. begin
  116. FProcSource.SetDDL;
  117. SQLEditorSource.Text := FProcSource.GetDDL;
  118. if FProcSource.SOURCE_TYPE = stPackage then
  119. SQLEditorBody.Text := FProcSource.GetBodyDDL;
  120. SQLEditor.Modified := false;
  121. end;
  122. SetErrorStatus;
  123. Show;
  124. end;
  125. procedure TProcedureEditorFrm.FormClose(Sender: TObject;
  126. var Action: TCloseAction);
  127. var
  128. answer: word;
  129. begin
  130. if SQLEditor.Modified then
  131. begin
  132. answer := MessageDlg('Would you like to save your changes to '+FProcSource.OWNER+'.'+FProcSource.SOURCE_NAME+' ?', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
  133. if answer = mrYes then btnCompile.Click;
  134. //if answer = mrNo then
  135. if answer = mrCancel then abort;
  136. end;
  137. with MainFrm.dxBarListWindows.Items do
  138. Delete(IndexOfObject(Self));
  139. action := caFree;
  140. end;
  141. {**************************** PROCEDURE EVENT *********************************}
  142. procedure TProcedureEditorFrm.btnNewQueryClick(Sender: TObject);
  143. var
  144. answer: word;
  145. begin
  146. if SQLEditor.Modified then
  147. begin
  148. answer := MessageDlg('Would you like to save your changes to '+FProcSource.OWNER+'.'+FProcSource.SOURCE_NAME+' ?', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
  149. if answer = mrYes then btnCompile.Click;
  150. if answer = mrNo then NewProcSource;
  151. if answer = mrCancel then abort;
  152. end;
  153. end;
  154. procedure TProcedureEditorFrm.NewProcSource;
  155. begin
  156. FProcSource := ProcedureDetailFrm.Init(FProcSource);
  157. SQLEditorSource.Text := FProcSource.GetDDL;
  158. if FProcSource.SOURCE_TYPE = stPackage then
  159. SQLEditorBody.Text := FProcSource.GetDefaultBodyDDL;
  160. end;
  161. procedure TProcedureEditorFrm.btnOpenQueryClick(Sender: TObject);
  162. begin
  163. OpenDialog.FileName := '';
  164. if OpenDialog.Execute then
  165. begin
  166. btnNewQuery.Click;
  167. SQLEditor.Lines.LoadFromFile(OpenDialog.FileName);
  168. Caption := FProcSource.OraSession.Server+'/'+FProcSource.OraSession.UserName+' - '+'Procedure Editor' +' ('+OpenDialog.FileName+')';
  169. with MainFrm.dxBarListWindows do
  170. Items[Items.IndexOfObject(Self)] := Self.Caption ;
  171. editorStatusBar.Panels[2].Text := 'Modified';
  172. end;
  173. end;
  174. procedure TProcedureEditorFrm.btnSaveQueryClick(Sender: TObject);
  175. begin
  176. if SaveDialog.Execute then
  177. begin
  178. SQLEditor.Lines.SaveToFile(SaveDialog.FileName);
  179. Caption := FProcSource.OraSession.Server+'/'+FProcSource.OraSession.Username+' - '+'Procedure Editor' +' ('+SaveDialog.FileName+')';
  180. with MainFrm.dxBarListWindows do
  181. Items[Items.IndexOfObject(Self)] := Self.Caption ;
  182. editorStatusBar.Panels[2].Text := '';
  183. end;
  184. end;
  185. function TProcedureEditorFrm.GetSQLEditor: TRichEdit;
  186. begin
  187. if pc.ActivePage = tsSource then
  188. result := SQLEditorSource
  189. else
  190. result := SQLEditorBody;
  191. end;
  192. function TProcedureEditorFrm.GetSQLEditorCol: Integer;
  193. begin
  194. with SQLEditor do
  195. Result := SelStart - SendMessage(Handle, EM_LINEINDEX, SQLEditorRow, 0);
  196. end;
  197. function TProcedureEditorFrm.GetSQLEditorRow: Integer;
  198. begin
  199. with SQLEditor do
  200. Result := SendMessage(Handle, EM_LINEFROMCHAR, SelStart, 0);
  201. end;
  202. procedure TProcedureEditorFrm.SQLEditorSourceChange(Sender: TObject);
  203. begin
  204. if FProcSource = nil then exit;
  205. SQLEditor.OnSelectionChange(SQLEditor);
  206. TdxStatusBarTextPanelStyle(editorStatusBar.Panels[1].PanelStyle).ImageIndex := 0;
  207. TdxStatusBarTextPanelStyle(editorStatusBar.Panels[0].PanelStyle).ImageIndex := 2;
  208. btnUndo.Enabled := SendMessage(SQLEditor.Handle, EM_CANUNDO, 0, 0) <> 0;
  209. btnRedo.Enabled := SendMessage(SQLEditor.Handle, EM_CANREDO, 0, 0) <> 0;
  210. if SQLEditor.Text = FProcSource.CODE then
  211. editorStatusBar.Panels[2].Text := ''
  212. else
  213. editorStatusBar.Panels[2].Text := 'Modified';
  214. end;
  215. procedure TProcedureEditorFrm.SQLEditorSourceSelectionChange(Sender: TObject);
  216. begin
  217. with SQLEditor do
  218. begin
  219. //FEditorUpdating := True;
  220. try
  221. editorStatusBar.Panels[1].Text := Format('Line: %3d Col: %3d', [1 + SQLEditorRow, 1 + SQLEditorCol]);
  222. btnCopy.Enabled := SelLength > 0;
  223. btnCut.Enabled := btnCopy.Enabled;
  224. btnPaste.Enabled := SendMessage(SQLEditor.Handle, EM_CANPASTE, 0, 0) <> 0;
  225. btnClear.Enabled := btnCopy.Enabled;
  226. btnCompile.Enabled := Lines.Text <> '';
  227. case Ord(Paragraph.Alignment) of
  228. 0: btnAlignLeft.Down := True;
  229. 1: btnAlignRight.Down := True;
  230. 2: btnAlignCenter.Down := True;
  231. end;
  232. finally
  233. //FEditorUpdating := False;
  234. end;
  235. end;
  236. end;
  237. procedure TProcedureEditorFrm.btnUndoClick(Sender: TObject);
  238. begin
  239. SendMessage(SQLEditor.Handle, EM_UNDO, 0, 0);
  240. end;
  241. procedure TProcedureEditorFrm.btnRedoClick(Sender: TObject);
  242. begin
  243. SendMessage(SQLEditor.Handle, EM_REDO, 0, 0);
  244. end;
  245. procedure TProcedureEditorFrm.btnCutClick(Sender: TObject);
  246. begin
  247. SQLEditor.CutToClipboard;
  248. end;
  249. procedure TProcedureEditorFrm.btnCopyClick(Sender: TObject);
  250. begin
  251. SQLEditor.CopyToClipboard;
  252. end;
  253. procedure TProcedureEditorFrm.btnPasteClick(Sender: TObject);
  254. begin
  255. SQLEditor.PasteFromClipboard;
  256. end;
  257. procedure TProcedureEditorFrm.btnClearClick(Sender: TObject);
  258. begin
  259. SQLEditor.ClearSelection;
  260. end;
  261. procedure TProcedureEditorFrm.btnSelectAllClick(Sender: TObject);
  262. begin
  263. SQLEditor.SelectAll;
  264. end;
  265. procedure TProcedureEditorFrm.btnFindTextClick(Sender: TObject);
  266. begin
  267. SQLEditor.SelLength := 0;
  268. FindDialog.Execute;
  269. end;
  270. procedure TProcedureEditorFrm.btnReplaceClick(Sender: TObject);
  271. begin
  272. SQLEditor.SelLength := 0;
  273. ReplaceDialog.Execute;
  274. end;
  275. procedure TProcedureEditorFrm.btnAlignLeftClick(Sender: TObject);
  276. begin
  277. if TdxBarButton(Sender).Down then
  278. SQLEditor.Paragraph.Alignment := TAlignment(TdxBarButton(Sender).Tag)
  279. else
  280. SQLEditor.Paragraph.Alignment := taLeftJustify;
  281. end;
  282. procedure TProcedureEditorFrm.FindDialogFind(Sender: TObject);
  283. var
  284. StartPos, FindLength, FoundAt: Integer;
  285. Flags: TSearchTypes;
  286. P: TPoint;
  287. CaretR, R, IntersectR: TRect;
  288. begin
  289. with SQLEditor, TFindDialog(Sender) do
  290. begin
  291. if frDown in Options then
  292. begin
  293. if SelLength = 0 then StartPos := SelStart
  294. else StartPos := SelStart + SelLength;
  295. FindLength := Length(Text) - StartPos;
  296. end
  297. else
  298. begin
  299. StartPos := SelStart;
  300. FindLength := -StartPos;
  301. end;
  302. Flags := [];
  303. if frMatchCase in Options then Include(Flags, stMatchCase);
  304. if frWholeWord in Options then Include(Flags, stWholeWord);
  305. Screen.Cursor := crHourglass;
  306. FoundAt := SQLEditor.FindText(FindText, StartPos, FindLength, Flags);
  307. if not (frReplaceAll in Options) then Screen.Cursor := crDefault;
  308. if FoundAt > -1 then
  309. if frReplaceAll in Options then
  310. begin
  311. SelStart := FoundAt;
  312. SelLength := Length(FindText);
  313. end
  314. else
  315. begin
  316. SetFocus;
  317. SelStart := FoundAt;
  318. SelLength := Length(FindText);
  319. GetCaretPos(P);
  320. P := ClientToScreen(P);
  321. CaretR := Rect(P.X, P.Y, P.X + 2, P.Y + 20);
  322. GetWindowRect(Handle, R);
  323. if IntersectRect(IntersectR, CaretR, R) then
  324. if P.Y < Screen.Height div 2 then
  325. Top := P.Y + 40
  326. else
  327. Top := P.Y - (R.Bottom - R.Top + 20);
  328. end
  329. else
  330. if not (frReplaceAll in Options) then
  331. Application.MessageBox('The search text is not found.',
  332. 'Information', MB_ICONINFORMATION);
  333. end;
  334. end;
  335. procedure TProcedureEditorFrm.ReplaceDialogFind(Sender: TObject);
  336. var
  337. ReplacedCount, OldSelStart, PrevSelStart: Integer;
  338. S: string;
  339. begin
  340. with SQLEditor, TReplaceDialog(Sender) do
  341. begin
  342. ReplacedCount := 0;
  343. OldSelStart := SelStart;
  344. if frReplaceAll in Options then
  345. Screen.Cursor := crHourglass;
  346. repeat
  347. if (SelLength > 0) and ((SelText = FindText) or
  348. (not (frMatchCase in Options) and
  349. (AnsiUpperCase(SelText) = AnsiUpperCase(FindText)))) then
  350. begin
  351. SelText := ReplaceText;
  352. Inc(ReplacedCount);
  353. end;
  354. PrevSelStart := SelStart;
  355. FindDialogFind(Sender);
  356. until not (frReplaceAll in Options) or (SelStart = PrevSelStart);
  357. if frReplaceAll in Options then
  358. begin
  359. Screen.Cursor := crDefault;
  360. if ReplacedCount = 0 then S := 'The search text is not found.'
  361. else
  362. begin
  363. SelStart := OldSelStart;
  364. S := Format('Replaced %d occurances.', [ReplacedCount]);
  365. end;
  366. Application.MessageBox(PChar(S), 'Information',
  367. MB_ICONINFORMATION);
  368. end;
  369. end;
  370. end;
  371. {**************************** PROCEDURE EVENT *********************************}
  372. procedure TProcedureEditorFrm.btnCompileClick(Sender: TObject);
  373. var
  374. isValid: boolean;
  375. begin
  376. if SQLEditor.Text = '' then exit;
  377. FProcSource.CODE := SQLEditorSource.Text;
  378. if FProcSource.SOURCE_TYPE = stPackage then
  379. FProcSource.BODY_CODE := SQLEditorBody.Text;
  380. isValid := FProcSource.AlterSource(FProcSource.CODE);
  381. if (isValid) and (FProcSource.SOURCE_TYPE = stPackage) then
  382. isValid := FProcSource.AlterSource(FProcSource.BODY_CODE);
  383. SetErrorStatus;
  384. SQLEditor.Modified := false;
  385. editorStatusBar.Panels[2].Text := '';
  386. if not isValid then editorStatusBar.Panels[3].Text := 'Invalid';
  387. end;
  388. procedure TProcedureEditorFrm.SetErrorStatus;
  389. begin
  390. lviewError.Items.Clear;
  391. if pc.ActivePage = tsSource then
  392. begin
  393. FillViewHorizontal(FProcSource.SourceErrors, lviewError);
  394. if FProcSource.SourceErrors.RecordCount = 0 then
  395. editorStatusBar.Panels[3].Text := 'Valid'
  396. else
  397. editorStatusBar.Panels[3].Text := 'Invalid';
  398. end;
  399. if pc.ActivePage = tsBody then
  400. begin
  401. FillViewHorizontal(FProcSource.SourceBodyErrors, lviewError);
  402. if FProcSource.SourceBodyErrors.RecordCount = 0 then
  403. editorStatusBar.Panels[3].Text := 'Valid'
  404. else
  405. editorStatusBar.Panels[3].Text := 'Invalid';
  406. end;
  407. end;
  408. procedure TProcedureEditorFrm.bbtnRunClick(Sender: TObject);
  409. begin
  410. ProcedureRunFrm.Init(FProcSource);
  411. end;
  412. procedure TProcedureEditorFrm.pcChange(Sender: TObject);
  413. begin
  414. SetErrorStatus;
  415. end;
  416. end.