Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / options / optjava.cxx
blob64ba41c3fe0bbf55551445684fb9f0da3f2bd8a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <o3tl/safeint.hxx>
21 #include <sal/config.h>
22 #include <sal/log.hxx>
24 #include <cassert>
25 #include <memory>
27 #include <config_java.h>
29 #include "optaboutconfig.hxx"
30 #include "optjava.hxx"
31 #include <treeopt.hxx>
32 #include <dialmgr.hxx>
34 #include <officecfg/Office/Common.hxx>
35 #include <osl/file.hxx>
36 #include <rtl/bootstrap.hxx>
38 #include <strings.hrc>
39 #include <vcl/svapp.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/urlobj.hxx>
42 #include <vcl/weld.hxx>
43 #include <unotools/pathoptions.hxx>
44 #include <svtools/imagemgr.hxx>
45 #include <svtools/restartdialog.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/inputdlg.hxx>
48 #include <comphelper/diagnose_ex.hxx>
49 #include <comphelper/processfactory.hxx>
50 #include <comphelper/string.hxx>
51 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
52 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
53 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
54 #if HAVE_FEATURE_JAVA
55 #include <jvmfwk/framework.hxx>
56 #endif
58 // define ----------------------------------------------------------------
60 #define CLASSPATH_DELIMITER SAL_PATHSEPARATOR
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::ucb;
64 using namespace ::com::sun::star::ui::dialogs;
65 using namespace ::com::sun::star::uno;
67 // class SvxJavaOptionsPage ----------------------------------------------
68 SvxJavaOptionsPage::SvxJavaOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
69 : SfxTabPage(pPage, pController, "cui/ui/optadvancedpage.ui", "OptAdvancedPage", &rSet)
70 , m_aResetIdle("cui options SvxJavaOptionsPage Reset")
71 , xDialogListener(new ::svt::DialogClosedListener())
72 , m_xJavaEnableCB(m_xBuilder->weld_check_button("javaenabled"))
73 , m_xJavaList(m_xBuilder->weld_tree_view("javas"))
74 , m_xJavaPathText(m_xBuilder->weld_label("javapath"))
75 , m_xAddBtn(m_xBuilder->weld_button("add"))
76 , m_xParameterBtn(m_xBuilder->weld_button("parameters"))
77 , m_xClassPathBtn(m_xBuilder->weld_button("classpath"))
78 , m_xExpertConfigBtn(m_xBuilder->weld_button("expertconfig"))
79 , m_xExperimentalCB(m_xBuilder->weld_check_button("experimental"))
80 , m_xMacroCB(m_xBuilder->weld_check_button("macrorecording"))
81 , m_xAddDialogText(m_xBuilder->weld_label("selectruntime"))
82 , m_xJavaFrame(m_xBuilder->weld_widget("javaframe"))
84 m_sInstallText = m_xJavaPathText->get_label();
85 m_sAddDialogText = m_xAddDialogText->get_label();
87 m_xJavaList->set_size_request(m_xJavaList->get_approximate_digit_width() * 30,
88 m_xJavaList->get_height_rows(8));
90 m_xJavaList->enable_toggle_buttons(weld::ColumnToggleType::Radio);
91 m_xJavaList->connect_toggled( LINK( this, SvxJavaOptionsPage, CheckHdl_Impl ) );
92 m_xJavaList->connect_changed( LINK( this, SvxJavaOptionsPage, SelectHdl_Impl ) );
94 std::vector<int> aWidths
96 m_xJavaList->get_checkbox_column_width(),
97 o3tl::narrowing<int>(m_xJavaList->get_pixel_size("Sun Microsystems Inc.").Width())
99 m_xJavaList->set_column_fixed_widths(aWidths);
101 m_xJavaEnableCB->connect_toggled( LINK( this, SvxJavaOptionsPage, EnableHdl_Impl ) );
102 m_xAddBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, AddHdl_Impl ) );
103 m_xParameterBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ParameterHdl_Impl ) );
104 m_xClassPathBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ClassPathHdl_Impl ) );
105 m_aResetIdle.SetInvokeHandler( LINK( this, SvxJavaOptionsPage, ResetHdl_Impl ) );
107 m_xExpertConfigBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ExpertConfigHdl_Impl) );
108 if (!officecfg::Office::Common::Security::EnableExpertConfiguration::get())
109 m_xExpertConfigBtn->set_sensitive(false);
111 if (officecfg::Office::Common::Misc::MacroRecorderMode::isReadOnly())
112 m_xMacroCB->set_sensitive(false);
114 if (officecfg::Office::Common::Misc::ExperimentalMode::isReadOnly())
115 m_xExperimentalCB->set_sensitive(false);
117 xDialogListener->SetDialogClosedLink( LINK( this, SvxJavaOptionsPage, DialogClosedHdl ) );
119 EnableHdl_Impl(*m_xJavaEnableCB);
120 #if HAVE_FEATURE_JAVA
121 jfw_lock();
122 #else
123 m_xJavaFrame->set_sensitive(false);
124 #endif
127 SvxJavaOptionsPage::~SvxJavaOptionsPage()
129 ClearJavaInfo();
130 #if HAVE_FEATURE_JAVA
131 m_aAddedInfos.clear();
133 jfw_unlock();
134 #endif
137 IMPL_LINK_NOARG(SvxJavaOptionsPage, EnableHdl_Impl, weld::Toggleable&, void)
139 bool bEnable = m_xJavaFrame->get_sensitive() && m_xJavaEnableCB->get_active();
140 m_xJavaList->set_sensitive(bEnable);
143 IMPL_LINK(SvxJavaOptionsPage, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
145 HandleCheckEntry(m_xJavaList->get_iter_index_in_parent(rRowCol.first));
148 IMPL_LINK_NOARG(SvxJavaOptionsPage, SelectHdl_Impl, weld::TreeView&, void)
150 UpdateJavaPathText();
153 IMPL_LINK_NOARG(SvxJavaOptionsPage, AddHdl_Impl, weld::Button&, void)
157 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
158 xFolderPicker = sfx2::createFolderPicker(xContext, GetFrameWeld());
160 OUString sWorkFolder = SvtPathOptions().GetWorkPath();
161 xFolderPicker->setDisplayDirectory( sWorkFolder );
162 xFolderPicker->setDescription( m_sAddDialogText );
164 Reference< XAsynchronousExecutableDialog > xAsyncDlg( xFolderPicker, UNO_QUERY );
165 if ( xAsyncDlg.is() )
166 xAsyncDlg->startExecuteModal( xDialogListener );
167 else if ( xFolderPicker.is() && xFolderPicker->execute() == ExecutableDialogResults::OK )
168 AddFolder( xFolderPicker->getDirectory() );
170 catch (const Exception&)
172 TOOLS_WARN_EXCEPTION( "cui.options", "SvxJavaOptionsPage::AddHdl_Impl()");
176 IMPL_LINK_NOARG(SvxJavaOptionsPage, ParameterHdl_Impl, weld::Button&, void)
178 #if HAVE_FEATURE_JAVA
179 std::vector< OUString > aParameterList;
180 if (!m_xParamDlg)
182 m_xParamDlg.reset(new SvxJavaParameterDlg(GetFrameWeld()));
183 javaFrameworkError eErr = jfw_getVMParameters( &m_parParameters );
184 if ( JFW_E_NONE == eErr && !m_parParameters.empty() )
186 aParameterList = m_parParameters;
187 m_xParamDlg->SetParameters( aParameterList );
190 else
192 aParameterList = m_xParamDlg->GetParameters();
193 m_xParamDlg->DisableButtons(); //disable add, edit and remove button when dialog is reopened
196 if (m_xParamDlg->run() == RET_OK)
198 if ( aParameterList != m_xParamDlg->GetParameters() )
200 aParameterList = m_xParamDlg->GetParameters();
201 if ( jfw_isVMRunning() )
203 RequestRestart( svtools::RESTART_REASON_ASSIGNING_JAVAPARAMETERS );
207 else
208 m_xParamDlg->SetParameters( aParameterList );
209 #else
210 (void) this; // Silence loplugin:staticmethods
211 #endif
215 IMPL_LINK_NOARG(SvxJavaOptionsPage, ClassPathHdl_Impl, weld::Button&, void)
217 #if HAVE_FEATURE_JAVA
218 OUString sClassPath;
220 if ( !m_xPathDlg )
222 m_xPathDlg.reset(new SvxJavaClassPathDlg(GetFrameWeld()));
223 javaFrameworkError eErr = jfw_getUserClassPath( &m_pClassPath );
224 if ( JFW_E_NONE == eErr )
226 sClassPath = m_pClassPath;
227 m_xPathDlg->SetClassPath( sClassPath );
230 else
231 sClassPath = m_xPathDlg->GetClassPath();
233 m_xPathDlg->SetFocus();
234 if (m_xPathDlg->run() == RET_OK)
237 if (m_xPathDlg->GetClassPath() != sClassPath)
239 sClassPath = m_xPathDlg->GetClassPath();
240 if ( jfw_isVMRunning() )
242 RequestRestart( svtools::RESTART_REASON_ASSIGNING_FOLDERS );
246 else
247 m_xPathDlg->SetClassPath( sClassPath );
248 #else
249 (void) this;
250 #endif
254 IMPL_LINK_NOARG(SvxJavaOptionsPage, ResetHdl_Impl, Timer *, void)
256 LoadJREs();
260 IMPL_LINK_NOARG(SvxJavaOptionsPage, StartFolderPickerHdl, void*, void)
264 Reference< XAsynchronousExecutableDialog > xAsyncDlg( xFolderPicker, UNO_QUERY );
265 if ( xAsyncDlg.is() )
266 xAsyncDlg->startExecuteModal( xDialogListener );
267 else if ( xFolderPicker.is() && xFolderPicker->execute() == ExecutableDialogResults::OK )
268 AddFolder( xFolderPicker->getDirectory() );
270 catch ( Exception const & )
272 TOOLS_WARN_EXCEPTION( "cui.options", "SvxJavaOptionsPage::StartFolderPickerHdl()" );
276 IMPL_LINK( SvxJavaOptionsPage, DialogClosedHdl, DialogClosedEvent*, pEvt, void )
278 if ( RET_OK == pEvt->DialogResult )
280 DBG_ASSERT( xFolderPicker.is(), "SvxJavaOptionsPage::DialogClosedHdl(): no folder picker" );
282 AddFolder( xFolderPicker->getDirectory() );
286 IMPL_LINK_NOARG(SvxJavaOptionsPage, ExpertConfigHdl_Impl, weld::Button&, void)
288 CuiAboutConfigTabPage aExpertConfigDlg(GetFrameWeld());
290 weld::WaitObject aWait(GetFrameWeld());
291 aExpertConfigDlg.Reset();//initialize and reset function
294 if (RET_OK == aExpertConfigDlg.run())
296 aExpertConfigDlg.FillItemSet();//save changes if there are any
300 void SvxJavaOptionsPage::ClearJavaInfo()
302 #if HAVE_FEATURE_JAVA
303 m_parJavaInfo.clear();
304 #else
305 (void) this;
306 #endif
309 void SvxJavaOptionsPage::LoadJREs()
311 #if HAVE_FEATURE_JAVA
312 weld::WaitObject aWaitObj(GetFrameWeld());
313 javaFrameworkError eErr = jfw_findAllJREs( &m_parJavaInfo );
314 if ( JFW_E_NONE == eErr )
316 for (auto const & pInfo: m_parJavaInfo)
318 AddJRE( pInfo.get() );
322 for (auto const & pInfo: m_aAddedInfos)
324 AddJRE( pInfo.get() );
327 std::unique_ptr<JavaInfo> pSelectedJava;
328 eErr = jfw_getSelectedJRE( &pSelectedJava );
329 if ( !(JFW_E_NONE == eErr && pSelectedJava) )
330 return;
332 sal_Int32 i = 0;
333 for (auto const & pCmpInfo: m_parJavaInfo)
335 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pSelectedJava.get() ) )
337 HandleCheckEntry(i);
338 UpdateJavaPathText();
339 break;
341 ++i;
343 #else
344 (void) this;
345 #endif
349 void SvxJavaOptionsPage::AddJRE( JavaInfo const * _pInfo )
351 #if HAVE_FEATURE_JAVA
352 int nPos = m_xJavaList->n_children();
353 m_xJavaList->append();
354 m_xJavaList->set_toggle(nPos, TRISTATE_FALSE);
355 m_xJavaList->set_text(nPos, _pInfo->sVendor, 1);
356 m_xJavaList->set_text(nPos, _pInfo->sVersion, 2);
358 INetURLObject aLocObj(_pInfo->sLocation);
359 OUString sLocation = aLocObj.getFSysPath(FSysStyle::Detect);
360 m_xJavaList->set_id(nPos, sLocation);
361 #else
362 (void) this;
363 (void)_pInfo;
364 #endif
367 void SvxJavaOptionsPage::HandleCheckEntry(int nCheckedRow)
369 m_xJavaList->select(nCheckedRow);
370 for (int i = 0, nCount = m_xJavaList->n_children(); i < nCount; ++i)
372 // we have radio button behavior -> so uncheck the other entries
373 m_xJavaList->set_toggle(i, i == nCheckedRow ? TRISTATE_TRUE : TRISTATE_FALSE);
377 void SvxJavaOptionsPage::UpdateJavaPathText()
379 assert(m_xJavaList->get_selected_index() != -1);
380 // set installation directory info
381 OUString sLocation = m_xJavaList->get_selected_id();
382 // tdf#80646 insert LTR mark after label
383 OUString sInfo = m_sInstallText + u"\u200E" + sLocation;
384 m_xJavaPathText->set_label(sInfo);
387 void SvxJavaOptionsPage::AddFolder( const OUString& _rFolder )
389 #if HAVE_FEATURE_JAVA
390 bool bStartAgain = true;
391 std::unique_ptr<JavaInfo> pInfo;
392 javaFrameworkError eErr = jfw_getJavaInfoByPath( _rFolder, &pInfo );
393 if ( JFW_E_NONE == eErr && pInfo )
395 sal_Int32 nPos = 0;
396 bool bFound = false;
397 for (auto const & pCmpInfo: m_parJavaInfo)
399 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pInfo.get() ) )
401 bFound = true;
402 break;
404 ++nPos;
407 if ( !bFound )
409 for (auto const & pCmpInfo: m_aAddedInfos)
411 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pInfo.get() ) )
413 bFound = true;
414 break;
416 ++nPos;
420 if ( !bFound )
422 jfw_addJRELocation( pInfo->sLocation );
423 AddJRE( pInfo.get() );
424 m_aAddedInfos.push_back( std::move(pInfo) );
425 nPos = m_xJavaList->n_children() - 1;
428 HandleCheckEntry(nPos);
429 UpdateJavaPathText();
430 bStartAgain = false;
432 else if ( JFW_E_NOT_RECOGNIZED == eErr )
434 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
435 VclMessageType::Warning, VclButtonsType::Ok,
436 CuiResId(RID_CUISTR_JRE_NOT_RECOGNIZED)));
437 xBox->run();
439 else if ( JFW_E_FAILED_VERSION == eErr )
441 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
442 VclMessageType::Warning, VclButtonsType::Ok,
443 CuiResId(RID_CUISTR_JRE_FAILED_VERSION)));
444 xBox->run();
447 if ( bStartAgain )
449 xFolderPicker->setDisplayDirectory( _rFolder );
450 Application::PostUserEvent( LINK( this, SvxJavaOptionsPage, StartFolderPickerHdl ) );
452 #else
453 (void) this;
454 (void)_rFolder;
455 #endif
458 void SvxJavaOptionsPage::RequestRestart(svtools::RestartReason eReason)
460 OfaTreeOptionsDialog* pParentDlg(static_cast<OfaTreeOptionsDialog*>(GetDialogController()));
461 if (pParentDlg)
462 pParentDlg->SetNeedsRestart(eReason);
465 std::unique_ptr<SfxTabPage> SvxJavaOptionsPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
467 return std::make_unique<SvxJavaOptionsPage>(pPage, pController, *rAttrSet);
470 bool SvxJavaOptionsPage::FillItemSet( SfxItemSet* /*rCoreSet*/ )
472 bool bModified = false;
474 if ( m_xExperimentalCB->get_state_changed_from_saved() )
476 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
477 comphelper::ConfigurationChanges::create());
478 officecfg::Office::Common::Misc::ExperimentalMode::set( m_xExperimentalCB->get_active(), xChanges );
479 xChanges->commit();
480 bModified = true;
481 RequestRestart( svtools::RESTART_REASON_EXP_FEATURES );
484 if ( m_xMacroCB->get_state_changed_from_saved() )
486 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
487 comphelper::ConfigurationChanges::create());
488 officecfg::Office::Common::Misc::MacroRecorderMode::set( m_xMacroCB->get_active(), xChanges );
489 xChanges->commit();
490 bModified = true;
493 #if HAVE_FEATURE_JAVA
494 javaFrameworkError eErr = JFW_E_NONE;
495 if (m_xParamDlg)
497 eErr = jfw_setVMParameters(m_xParamDlg->GetParameters());
498 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setVMParameters");
499 bModified = true;
502 if (m_xPathDlg)
504 OUString sPath(m_xPathDlg->GetClassPath());
505 if (m_xPathDlg->GetOldPath() != sPath)
507 eErr = jfw_setUserClassPath( sPath );
508 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setUserClassPath");
509 bModified = true;
513 sal_uInt32 nCount = m_xJavaList->n_children();
514 for (sal_uInt32 i = 0; i < nCount; ++i)
516 if (m_xJavaList->get_toggle(i) == TRISTATE_TRUE)
518 JavaInfo const * pInfo;
519 if ( i < m_parJavaInfo.size() )
520 pInfo = m_parJavaInfo[i].get();
521 else
522 pInfo = m_aAddedInfos[ i - m_parJavaInfo.size() ].get();
524 std::unique_ptr<JavaInfo> pSelectedJava;
525 eErr = jfw_getSelectedJRE( &pSelectedJava );
526 if ( JFW_E_NONE == eErr || JFW_E_INVALID_SETTINGS == eErr )
528 if (!pSelectedJava || !jfw_areEqualJavaInfo( pInfo, pSelectedJava.get() ) )
530 if ( jfw_isVMRunning() ||
531 ( ( pInfo->nRequirements & JFW_REQUIRE_NEEDRESTART ) == JFW_REQUIRE_NEEDRESTART ) )
533 RequestRestart( svtools::RESTART_REASON_JAVA );
536 eErr = jfw_setSelectedJRE( pInfo );
537 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setSelectedJRE");
538 bModified = true;
541 break;
545 bool bEnabled = false;
546 eErr = jfw_getEnabled( &bEnabled );
547 DBG_ASSERT( JFW_E_NONE == eErr,
548 "SvxJavaOptionsPage::FillItemSet(): error in jfw_getEnabled" );
549 if ( bEnabled != m_xJavaEnableCB->get_active() )
551 eErr = jfw_setEnabled( m_xJavaEnableCB->get_active() );
552 DBG_ASSERT( JFW_E_NONE == eErr,
553 "SvxJavaOptionsPage::FillItemSet(): error in jfw_setEnabled" );
554 bModified = true;
556 #endif
558 return bModified;
562 void SvxJavaOptionsPage::Reset( const SfxItemSet* /*rSet*/ )
564 ClearJavaInfo();
565 m_xJavaList->clear();
567 #if HAVE_FEATURE_JAVA
568 bool bEnabled = false;
569 javaFrameworkError eErr = jfw_getEnabled( &bEnabled );
570 if (eErr == JFW_E_DIRECT_MODE)
572 // direct mode disregards Java settings made here, so gray them out
573 m_xJavaFrame->set_sensitive(false);
574 // check whether a usable JRE was set
575 std::unique_ptr<JavaInfo> pSelectedJava;
576 eErr = jfw_getSelectedJRE( &pSelectedJava );
577 bEnabled = (eErr == JFW_E_NONE);
579 else if ( eErr != JFW_E_NONE )
580 bEnabled = false;
581 m_xJavaEnableCB->set_active(bEnabled);
582 EnableHdl_Impl(*m_xJavaEnableCB);
583 #else
584 m_xJavaEnableCB->set_active(false);
585 m_xJavaEnableCB->set_sensitive(false);
586 #endif
588 m_xExperimentalCB->set_active( officecfg::Office::Common::Misc::ExperimentalMode::get() );
589 m_xExperimentalCB->save_state();
590 m_xMacroCB->set_active( officecfg::Office::Common::Misc::MacroRecorderMode::get() );
591 m_xMacroCB->save_state();
593 m_aResetIdle.Start();
597 void SvxJavaOptionsPage::FillUserData()
599 SetUserData( OUString() );
602 // class SvxJavaParameterDlg ---------------------------------------------
604 SvxJavaParameterDlg::SvxJavaParameterDlg(weld::Window* pParent)
605 : GenericDialogController(pParent, "cui/ui/javastartparametersdialog.ui",
606 "JavaStartParameters")
607 , m_xParameterEdit(m_xBuilder->weld_entry("parameterfield"))
608 , m_xAssignBtn(m_xBuilder->weld_button("assignbtn"))
609 , m_xAssignedList(m_xBuilder->weld_tree_view("assignlist"))
610 , m_xRemoveBtn(m_xBuilder->weld_button("removebtn"))
611 , m_xEditBtn(m_xBuilder->weld_button("editbtn"))
613 m_xAssignedList->set_size_request(m_xAssignedList->get_approximate_digit_width() * 54,
614 m_xAssignedList->get_height_rows(6));
615 m_xParameterEdit->connect_changed( LINK( this, SvxJavaParameterDlg, ModifyHdl_Impl ) );
616 m_xParameterEdit->connect_activate(LINK(this, SvxJavaParameterDlg, ActivateHdl_Impl));
617 m_xAssignBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, AssignHdl_Impl ) );
618 m_xRemoveBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, RemoveHdl_Impl ) );
619 m_xEditBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, EditHdl_Impl ) );
620 m_xAssignedList->connect_changed( LINK( this, SvxJavaParameterDlg, SelectHdl_Impl ) );
621 m_xAssignedList->connect_row_activated( LINK( this, SvxJavaParameterDlg, DblClickHdl_Impl ) );
623 ModifyHdl_Impl(*m_xParameterEdit);
624 EnableEditButton();
625 EnableRemoveButton();
628 SvxJavaParameterDlg::~SvxJavaParameterDlg()
632 IMPL_LINK_NOARG(SvxJavaParameterDlg, ModifyHdl_Impl, weld::Entry&, void)
634 OUString sParam = comphelper::string::strip(m_xParameterEdit->get_text(), ' ');
635 m_xAssignBtn->set_sensitive(!sParam.isEmpty());
638 IMPL_LINK_NOARG(SvxJavaParameterDlg, AssignHdl_Impl, weld::Button&, void)
640 OUString sParam = comphelper::string::strip(m_xParameterEdit->get_text(), ' ');
641 if (sParam.isEmpty())
642 return;
644 int nPos = m_xAssignedList->find_text(sParam);
645 if (nPos == -1)
647 m_xAssignedList->append_text(sParam);
648 m_xAssignedList->select(m_xAssignedList->n_children() - 1);
650 else
651 m_xAssignedList->select(nPos);
652 m_xParameterEdit->set_text(OUString());
653 ModifyHdl_Impl(*m_xParameterEdit);
654 EnableEditButton();
655 EnableRemoveButton();
658 IMPL_LINK_NOARG(SvxJavaParameterDlg, ActivateHdl_Impl, weld::Entry&, bool)
660 if (m_xAssignBtn->get_sensitive())
662 AssignHdl_Impl(*m_xAssignBtn);
663 return true;
665 return false;
668 IMPL_LINK_NOARG(SvxJavaParameterDlg, EditHdl_Impl, weld::Button&, void)
670 EditParameter();
673 IMPL_LINK_NOARG(SvxJavaParameterDlg, SelectHdl_Impl, weld::TreeView&, void)
675 EnableEditButton();
676 EnableRemoveButton();
679 IMPL_LINK_NOARG(SvxJavaParameterDlg, DblClickHdl_Impl, weld::TreeView&, bool)
681 EditParameter();
682 return true;
685 IMPL_LINK_NOARG(SvxJavaParameterDlg, RemoveHdl_Impl, weld::Button&, void)
687 int nPos = m_xAssignedList->get_selected_index();
688 if (nPos != -1)
690 m_xAssignedList->remove(nPos);
691 int nCount = m_xAssignedList->n_children();
692 if (nCount)
694 if (nPos >= nCount)
695 nPos = nCount - 1;
696 m_xAssignedList->select(nPos);
698 else
700 DisableEditButton();
703 EnableRemoveButton();
706 void SvxJavaParameterDlg::EditParameter()
708 int nPos = m_xAssignedList->get_selected_index();
709 m_xParameterEdit->set_text(OUString());
711 if (nPos == -1)
712 return;
714 InputDialog aParamEditDlg(m_xDialog.get(), CuiResId(RID_CUISTR_JAVA_START_PARAM));
715 OUString editableClassPath = m_xAssignedList->get_selected_text();
716 aParamEditDlg.SetEntryText(editableClassPath);
717 aParamEditDlg.HideHelpBtn();
719 if (!aParamEditDlg.run())
720 return;
721 OUString editedClassPath = comphelper::string::strip(aParamEditDlg.GetEntryText(), ' ');
723 if ( !editedClassPath.isEmpty() && editableClassPath != editedClassPath )
725 m_xAssignedList->remove(nPos);
726 m_xAssignedList->insert_text(nPos, editedClassPath);
727 m_xAssignedList->select(nPos);
731 short SvxJavaParameterDlg::run()
733 m_xParameterEdit->grab_focus();
734 m_xAssignedList->select(-1);
735 return GenericDialogController::run();
738 std::vector< OUString > SvxJavaParameterDlg::GetParameters() const
740 int nCount = m_xAssignedList->n_children();
741 std::vector< OUString > aParamList;
742 aParamList.reserve(nCount);
743 for (int i = 0; i < nCount; ++i)
744 aParamList.push_back(m_xAssignedList->get_text(i));
745 return aParamList;
748 void SvxJavaParameterDlg::DisableButtons()
750 DisableAssignButton();
751 DisableEditButton();
752 DisableRemoveButton();
755 void SvxJavaParameterDlg::SetParameters( std::vector< OUString > const & rParams )
757 m_xAssignedList->clear();
758 for (auto const & sParam: rParams)
760 m_xAssignedList->append_text(sParam);
762 DisableEditButton();
763 DisableRemoveButton();
767 // class SvxJavaClassPathDlg ---------------------------------------------
769 SvxJavaClassPathDlg::SvxJavaClassPathDlg(weld::Window* pParent)
770 : GenericDialogController(pParent, "cui/ui/javaclasspathdialog.ui", "JavaClassPath")
771 , m_xPathList(m_xBuilder->weld_tree_view("paths"))
772 , m_xAddArchiveBtn(m_xBuilder->weld_button("archive"))
773 , m_xAddPathBtn(m_xBuilder->weld_button("folder"))
774 , m_xRemoveBtn(m_xBuilder->weld_button("remove"))
776 m_xPathList->set_size_request(m_xPathList->get_approximate_digit_width() * 54,
777 m_xPathList->get_height_rows(8));
778 m_xAddArchiveBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, AddArchiveHdl_Impl ) );
779 m_xAddPathBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, AddPathHdl_Impl ) );
780 m_xRemoveBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, RemoveHdl_Impl ) );
781 m_xPathList->connect_changed( LINK( this, SvxJavaClassPathDlg, SelectHdl_Impl ) );
783 // set initial focus to path list
784 m_xPathList->grab_focus();
787 SvxJavaClassPathDlg::~SvxJavaClassPathDlg()
791 IMPL_LINK_NOARG(SvxJavaClassPathDlg, AddArchiveHdl_Impl, weld::Button&, void)
793 sfx2::FileDialogHelper aDlg(TemplateDescription::FILEOPEN_SIMPLE, FileDialogFlags::NONE, m_xDialog.get());
794 aDlg.SetTitle( CuiResId( RID_CUISTR_ARCHIVE_TITLE ) );
795 aDlg.AddFilter( CuiResId( RID_CUISTR_ARCHIVE_HEADLINE ), "*.jar;*.zip" );
796 aDlg.SetContext(sfx2::FileDialogHelper::JavaClassPath);
797 OUString sFolder;
798 if (m_xPathList->count_selected_rows() > 0)
800 osl::FileBase::getFileURLFromSystemPath(m_xPathList->get_selected_text(), sFolder);
801 // best effort
803 if (!sFolder.isEmpty())
804 aDlg.SetDisplayDirectory( sFolder );
805 if ( aDlg.Execute() == ERRCODE_NONE )
807 OUString sURL = aDlg.GetPath();
808 OUString sFile;
809 if (osl::FileBase::getSystemPathFromFileURL(sURL, sFile) == osl::FileBase::E_None)
811 INetURLObject aURL( sURL );
812 if ( !IsPathDuplicate( sFile ) )
814 m_xPathList->append("", sFile, SvFileInformationManager::GetImageId(aURL));
815 m_xPathList->select(m_xPathList->n_children() - 1);
817 else
819 OUString sMsg( CuiResId( RID_CUISTR_MULTIFILE_DBL_ERR ) );
820 sMsg = sMsg.replaceFirst( "%1", sFile );
821 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
822 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
823 xBox->run();
826 else
828 OUString sMsg( CuiResId( RID_CUISTR_CANNOTCONVERTURL_ERR ) );
829 sMsg = sMsg.replaceFirst( "%1", sURL );
830 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
831 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
832 xBox->run();
835 EnableRemoveButton();
838 IMPL_LINK_NOARG(SvxJavaClassPathDlg, AddPathHdl_Impl, weld::Button&, void)
840 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
841 Reference < XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
843 OUString sOldFolder;
844 if (m_xPathList->count_selected_rows() > 0)
846 osl::FileBase::getFileURLFromSystemPath(m_xPathList->get_selected_text(), sOldFolder);
847 // best effort
849 if (sOldFolder.isEmpty())
850 sOldFolder = SvtPathOptions().GetWorkPath();
851 xFolderPicker->setDisplayDirectory( sOldFolder );
852 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
854 OUString sFolderURL( xFolderPicker->getDirectory() );
855 INetURLObject aURL( sFolderURL );
856 OUString sNewFolder;
857 if (osl::FileBase::getSystemPathFromFileURL(sFolderURL, sNewFolder)
858 == osl::FileBase::E_None)
860 if ( !IsPathDuplicate( sNewFolder ) )
862 m_xPathList->append("", sNewFolder, SvFileInformationManager::GetImageId(aURL));
863 m_xPathList->select(m_xPathList->n_children() - 1);
865 else
867 OUString sMsg( CuiResId( RID_CUISTR_MULTIFILE_DBL_ERR ) );
868 sMsg = sMsg.replaceFirst( "%1", sNewFolder );
869 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
870 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
871 xBox->run();
874 else
876 OUString sMsg( CuiResId( RID_CUISTR_CANNOTCONVERTURL_ERR ) );
877 sMsg = sMsg.replaceFirst( "%1", sFolderURL );
878 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
879 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
880 xBox->run();
883 EnableRemoveButton();
886 IMPL_LINK_NOARG(SvxJavaClassPathDlg, RemoveHdl_Impl, weld::Button&, void)
888 int nPos = m_xPathList->get_selected_index();
889 if (nPos != -1)
891 m_xPathList->remove(nPos);
892 int nCount = m_xPathList->n_children();
893 if (nCount)
895 if (nPos >= nCount)
896 nPos = nCount - 1;
897 m_xPathList->select( nPos );
901 EnableRemoveButton();
904 IMPL_LINK_NOARG(SvxJavaClassPathDlg, SelectHdl_Impl, weld::TreeView&, void)
906 EnableRemoveButton();
909 bool SvxJavaClassPathDlg::IsPathDuplicate( std::u16string_view _rPath )
911 bool bRet = false;
912 int nCount = m_xPathList->n_children();
913 for (int i = 0; i < nCount; ++i)
915 if ( m_xPathList->get_text(i) == _rPath )
917 bRet = true;
918 break;
922 return bRet;
925 OUString SvxJavaClassPathDlg::GetClassPath() const
927 OUStringBuffer sPath;
928 int nCount = m_xPathList->n_children();
929 for (int i = 0; i < nCount; ++i)
931 if (!sPath.isEmpty())
932 sPath.append(CLASSPATH_DELIMITER);
933 sPath.append(m_xPathList->get_text(i));
935 return sPath.makeStringAndClear();
938 #if HAVE_FEATURE_JAVA
939 void SvxJavaClassPathDlg::SetClassPath( const OUString& _rPath )
941 if ( m_sOldPath.isEmpty() )
942 m_sOldPath = _rPath;
943 m_xPathList->clear();
944 if (!_rPath.isEmpty())
946 std::vector paths = jfw_convertUserPathList(_rPath);
947 for (auto const& path : paths)
949 OUString sURL;
950 if (path.startsWith("$"))
952 sURL = path;
953 rtl::Bootstrap::expandMacros(sURL);
955 else
957 osl::FileBase::getFileURLFromSystemPath(path, sURL);
959 INetURLObject aURL( sURL );
960 m_xPathList->append("", path, SvFileInformationManager::GetImageId(aURL));
962 // select first entry
963 m_xPathList->select(0);
965 SelectHdl_Impl(*m_xPathList);
967 #endif
969 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */