BrookIDEIntegration.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Integrates Brook to Delphi or Lazarus IDE. }
  26. unit BrookIDEIntegration;
  27. {$I BrookDefines.inc}
  28. {$IFDEF FPC}
  29. {$WARN 5024 OFF}
  30. {$ENDIF}
  31. interface
  32. uses
  33. SysUtils,
  34. Classes,
  35. TypInfo,
  36. Dialogs,
  37. {$IFDEF LCL}
  38. PropEdits,
  39. ComponentEditors,
  40. {$ELSE}
  41. DesignIntf,
  42. DesignEditors,
  43. ColnEdit,
  44. {$ENDIF}
  45. libsagui,
  46. BrookUtility;
  47. resourcestring
  48. SBrookSelectLibraryTitle = 'Select library ...';
  49. SBrookSharedLibraryFilter = 'Shared libraries (%s)|%s|All files (*.*)|*.*';
  50. SBrookURLEntryPointsEditor = 'URL entry-points editor ...';
  51. SBrookURLRoutesEditor = 'URL routes editor ...';
  52. type
  53. { TBrookLibraryNamePropertyEditor }
  54. TBrookLibraryNamePropertyEditor = class(
  55. {$IFDEF LCL}TFileNamePropertyEditor{$ELSE}TStringProperty{$ENDIF})
  56. public
  57. {$IFDEF LCL}
  58. function GetVerbCount: Integer; override;
  59. function GetVerb(AIndex: Integer): string; override;
  60. procedure ExecuteVerb(AIndex: Integer); override;
  61. {$ENDIF}
  62. function GetFilter: string;{$IFDEF LCL}override{$ELSE}virtual{$ENDIF};
  63. function GetDialogTitle: string;{$IFDEF LCL}override{$ELSE}virtual{$ENDIF};
  64. {$IFNDEF LCL}
  65. function CreateFileDialog: TOpenDialog; virtual;
  66. procedure Edit; override;
  67. function GetAttributes: TPropertyAttributes; override;
  68. function GetDialogOptions: TOpenOptions; virtual;
  69. function GetInitialDirectory: string; virtual;
  70. procedure SetFileName(const AFileName: string); virtual;
  71. {$ENDIF}
  72. end;
  73. { TBrookHTTPRequestMethodsPropertyEditor }
  74. TBrookHTTPRequestMethodsPropertyEditor = class(TSetProperty)
  75. public
  76. procedure GetProperties(AProc:
  77. {$IFDEF LCL}TGetPropEditProc{$ELSE}TGetPropProc{$ENDIF}); override;
  78. end;
  79. { TBrookLibraryNameComponentEditor }
  80. TBrookLibraryNameComponentEditor = class(TDefaultEditor)
  81. public
  82. procedure Edit; override;
  83. function GetVerb(AIndex: Integer): string; override;
  84. {$IFNDEF LCL}
  85. function GetVerbCount: Integer; override;
  86. {$ENDIF}
  87. procedure ExecuteVerb(AIndex: Integer); override;
  88. end;
  89. { TBrookOnMathExtensionComponentEditor }
  90. TBrookOnMathExtensionComponentEditor = class(TDefaultEditor)
  91. protected
  92. procedure EditProperty(const AProperty:
  93. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF};
  94. var AContinue: Boolean); override;
  95. end;
  96. { TBrookOnHTTPRequestComponentEditor }
  97. TBrookOnHTTPRequestComponentEditor = class(TDefaultEditor)
  98. protected
  99. procedure EditProperty(const AProperty:
  100. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF};
  101. var AContinue: Boolean); override;
  102. end;
  103. { TBrookURLRouterComponentEditor }
  104. TBrookURLRouterComponentEditor = class(TComponentEditor)
  105. public
  106. procedure ExecuteVerb(AIndex: Integer); override;
  107. function GetVerb(AIndex: Integer): string; override;
  108. function GetVerbCount: Integer; override;
  109. end;
  110. { TBrookURLEntryPointsComponentEditor }
  111. TBrookURLEntryPointsComponentEditor = class(TComponentEditor)
  112. public
  113. procedure ExecuteVerb(AIndex: Integer); override;
  114. function GetVerb(AIndex: Integer): string; override;
  115. function GetVerbCount: Integer; override;
  116. end;
  117. {$R BrookTardigradeIcons.res}
  118. procedure Register;
  119. implementation
  120. uses
  121. BrookLibraryLoader,
  122. BrookLogger,
  123. BrookMathExpression,
  124. BrookMediaTypes,
  125. BrookURLEntryPoints,
  126. BrookURLRouter,
  127. BrookHTTPServer;
  128. {$IFNDEF LCL}
  129. type
  130. TLocalSetElementProperty = class(TSetElementProperty)
  131. public
  132. constructor Create(AParent: TPropertyEditor; AElement: Integer); reintroduce;
  133. end;
  134. constructor TLocalSetElementProperty.Create(AParent: TPropertyEditor;
  135. AElement: Integer);
  136. begin
  137. inherited Create(AParent, AElement);
  138. end;
  139. function BrookHTTPRequestMethodsPropertyMapper(AObj: TPersistent;
  140. APropInfo: PPropInfo): TPropertyEditorClass;
  141. begin
  142. if Assigned(AObj) and (AObj is TBrookURLRoute) and
  143. SameText(APropInfo.NameFld.ToString, 'Methods') then
  144. Exit(TBrookHTTPRequestMethodsPropertyEditor);
  145. Result := nil;
  146. end;
  147. {$ENDIF}
  148. procedure Register;
  149. begin
  150. RegisterComponents('Brook', [
  151. TBrookLibraryLoader,
  152. TBrookLogger,
  153. TBrookMathExpression,
  154. TBrookMIME,
  155. TBrookURLEntryPoints,
  156. TBrookURLRouter,
  157. TBrookHTTPServer
  158. ]);
  159. RegisterPropertyEditor(TypeInfo(TFileName), TBrookLibraryLoader,
  160. 'LibraryName', TBrookLibraryNamePropertyEditor);
  161. {$IFDEF LCL}
  162. RegisterPropertyEditor(TypeInfo(TFileName), TBrookMIME,
  163. 'FileName', TFileNamePropertyEditor);
  164. RegisterPropertyEditor(TypeInfo(string), TBrookHTTPServerSecurity,
  165. 'PrivatePassword', TPasswordStringPropertyEditor);
  166. RegisterPropertyEditor(TypeInfo(TBrookHTTPRequestMethods), nil, '',
  167. TBrookHTTPRequestMethodsPropertyEditor);
  168. RegisterPropertyEditor(TypeInfo(string), TBrookHTTPServer, 'UploadsDir',
  169. TDirectoryPropertyEditor);
  170. {$ELSE}
  171. RegisterPropertyMapper(BrookHTTPRequestMethodsPropertyMapper);
  172. {$ENDIF}
  173. RegisterComponentEditor(TBrookLibraryLoader, TBrookLibraryNameComponentEditor);
  174. RegisterComponentEditor(TBrookMathExpression, TBrookOnMathExtensionComponentEditor);
  175. RegisterComponentEditor(TBrookURLEntryPoints, TBrookURLEntryPointsComponentEditor);
  176. RegisterComponentEditor(TBrookURLRouter, TBrookURLRouterComponentEditor);
  177. RegisterComponentEditor(TBrookHTTPServer, TBrookOnHTTPRequestComponentEditor);
  178. end;
  179. {$IFDEF LCL}
  180. { TBrookLibraryNamePropertyEditor }
  181. function TBrookLibraryNamePropertyEditor.GetVerbCount: Integer;
  182. begin
  183. Result := 1;
  184. end;
  185. function TBrookLibraryNamePropertyEditor.GetVerb(AIndex: Integer): string;
  186. begin
  187. Result := SBrookSelectLibraryTitle;
  188. end;
  189. procedure TBrookLibraryNamePropertyEditor.ExecuteVerb(AIndex: Integer);
  190. begin
  191. Edit;
  192. end;
  193. {$ENDIF}
  194. function TBrookLibraryNamePropertyEditor.GetFilter: string;
  195. var
  196. VSharedSuffix: string;
  197. begin
  198. VSharedSuffix := Concat('*.', SharedSuffix);
  199. Result := Format(SBrookSharedLibraryFilter, [VSharedSuffix,
  200. {$IFDEF LINUX}Concat({$ENDIF}VSharedSuffix
  201. {$IFDEF LINUX}, ';', VSharedSuffix, '.*'){$ENDIF}]);
  202. end;
  203. function TBrookLibraryNamePropertyEditor.GetDialogTitle: string;
  204. begin
  205. Result := SBrookSelectLibraryTitle;
  206. end;
  207. {$IFNDEF LCL}
  208. function TBrookLibraryNamePropertyEditor.CreateFileDialog: TOpenDialog;
  209. begin
  210. Result := TOpenDialog.Create(nil);
  211. end;
  212. procedure TBrookLibraryNamePropertyEditor.Edit;
  213. begin
  214. with CreateFileDialog do
  215. try
  216. Filter := GetFilter;
  217. Options := GetDialogOptions;
  218. FileName := GetStrValue;
  219. InitialDir := GetInitialDirectory;
  220. Title := GetDialogTitle;
  221. if Execute then
  222. SetFileName(FileName);
  223. finally
  224. Free;
  225. end;
  226. end;
  227. function TBrookLibraryNamePropertyEditor.GetAttributes: TPropertyAttributes;
  228. begin
  229. Result := [paDialog, paRevertable];
  230. end;
  231. function TBrookLibraryNamePropertyEditor.GetDialogOptions: TOpenOptions;
  232. begin
  233. Result := [ofEnableSizing];
  234. end;
  235. function TBrookLibraryNamePropertyEditor.GetInitialDirectory: string;
  236. begin
  237. Result := '';
  238. end;
  239. procedure TBrookLibraryNamePropertyEditor.SetFileName(
  240. const AFileName: string);
  241. begin
  242. SetStrValue(AFileName);
  243. end;
  244. {$ENDIF}
  245. { TBrookHTTPRequestMethodsPropertyEditor }
  246. procedure TBrookHTTPRequestMethodsPropertyEditor.GetProperties(
  247. AProc:{$IFDEF LCL}TGetPropEditProc{$ELSE}TGetPropProc{$ENDIF});
  248. var
  249. M: TBrookHTTPRequestMethod;
  250. {$IFNDEF LCL}
  251. P: IProperty;
  252. {$ENDIF}
  253. begin
  254. for M := Succ(Low(TBrookHTTPRequestMethod)) to High(TBrookHTTPRequestMethod) do
  255. {$IFDEF LCL}
  256. AProc(TSetElementProperty.Create(Self, Ord(M)));
  257. {$ELSE}
  258. begin
  259. P := TLocalSetElementProperty.Create(Self, Ord(M));
  260. AProc(P);
  261. P := nil;
  262. end;
  263. {$ENDIF}
  264. end;
  265. { TBrookLibraryNameComponentEditor }
  266. procedure TBrookLibraryNameComponentEditor.Edit;
  267. var
  268. VDialog: TOpenDialog;
  269. VLibraryLoader: TBrookLibraryLoader;
  270. VPropertyEditor: TBrookLibraryNamePropertyEditor;
  271. begin
  272. VLibraryLoader := Component as TBrookLibraryLoader;
  273. if not Assigned(VLibraryLoader) then
  274. Exit;
  275. VPropertyEditor := TBrookLibraryNamePropertyEditor.Create(nil, 0);
  276. VDialog := VPropertyEditor.CreateFileDialog;
  277. try
  278. VDialog.Filter := VPropertyEditor.GetFilter;
  279. VDialog.Options := VPropertyEditor.GetDialogOptions;
  280. VDialog.InitialDir := VPropertyEditor.GetInitialDirectory;
  281. VDialog.Title := VPropertyEditor.GetDialogTitle;
  282. VDialog.FileName := VLibraryLoader.LibraryName;
  283. if VDialog.Execute then
  284. begin
  285. VLibraryLoader.LibraryName := VDialog.FileName;
  286. Designer.Modified;
  287. end;
  288. finally
  289. VPropertyEditor.Free;
  290. end;
  291. end;
  292. function TBrookLibraryNameComponentEditor.GetVerb(
  293. AIndex: Integer): string;
  294. begin
  295. Result := SBrookSelectLibraryTitle;
  296. end;
  297. {$IFNDEF LCL}
  298. function TBrookLibraryNameComponentEditor.GetVerbCount: Integer;
  299. begin
  300. Result := 1;
  301. end;
  302. {$ENDIF}
  303. procedure TBrookLibraryNameComponentEditor.ExecuteVerb(AIndex: Integer);
  304. begin
  305. Edit;
  306. end;
  307. { TBrookOnMathExtensionComponentEditor }
  308. procedure TBrookOnMathExtensionComponentEditor.EditProperty(const AProperty:
  309. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF}; var AContinue: Boolean);
  310. begin
  311. if SameText(AProperty.GetName, 'OnExtension') then
  312. inherited EditProperty(AProperty, AContinue);
  313. end;
  314. { TBrookOnHTTPRequestComponentEditor }
  315. procedure TBrookOnHTTPRequestComponentEditor.EditProperty(const AProperty:
  316. {$IFDEF LCL}TPropertyEditor{$ELSE}IProperty{$ENDIF}; var AContinue: Boolean);
  317. begin
  318. if SameText(AProperty.GetName, 'OnRequest') then
  319. inherited EditProperty(AProperty, AContinue);
  320. end;
  321. { TBrookURLRouterComponentEditor }
  322. procedure TBrookURLRouterComponentEditor.ExecuteVerb(AIndex: Integer);
  323. var
  324. VRouter: TBrookURLRouter;
  325. begin
  326. VRouter := GetComponent as TBrookURLRouter;
  327. {$IFDEF LCL}
  328. EditCollection(
  329. {$ELSE}
  330. ShowCollectionEditor(Designer,
  331. {$ENDIF}
  332. VRouter, VRouter.Routes, 'Routes');
  333. end;
  334. function TBrookURLRouterComponentEditor.GetVerb(AIndex: Integer): string;
  335. begin
  336. Result := SBrookURLRoutesEditor;
  337. end;
  338. function TBrookURLRouterComponentEditor.GetVerbCount: Integer;
  339. begin
  340. Result := 1;
  341. end;
  342. { TBrookURLEntryPointsComponentEditor }
  343. procedure TBrookURLEntryPointsComponentEditor.ExecuteVerb(AIndex: Integer);
  344. var
  345. VEntryPoints: TBrookURLEntryPoints;
  346. begin
  347. VEntryPoints := GetComponent as TBrookURLEntryPoints;
  348. {$IFDEF LCL}
  349. EditCollection(
  350. {$ELSE}
  351. ShowCollectionEditor(Designer,
  352. {$ENDIF}
  353. VEntryPoints, VEntryPoints.List, 'List');
  354. end;
  355. function TBrookURLEntryPointsComponentEditor.GetVerb(AIndex: Integer): string;
  356. begin
  357. Result := SBrookURLEntryPointsEditor;
  358. end;
  359. function TBrookURLEntryPointsComponentEditor.GetVerbCount: Integer;
  360. begin
  361. Result := 1;
  362. end;
  363. end.